js,jQuery初学易出现问题

来源:互联网 发布:世界十大灾难片 知乎 编辑:程序博客网 时间:2024/06/03 16:01

1.js给对象绑定事件时,应该在对象加载完成后绑定

//第一种方法,页面完全加载完绑定window.onload=function(){document.getElementById("search_button").onclick=function(){alert("hahahha")};}

<pre name="code" class="javascript">//第二种方法,加载完对象进行绑定<body><div id="search_div"><input type="text" id="search_text" ></input><input id="search_button" type="button" value="search" ></input><script type="text/javascript"><pre name="code" class="javascript"> document.getElementById("search_button").onclick=function(){alert("hahahha")}</script></div></body>

2.jQuery引入顺序应该在自定义的js代码上面否则自定义的代码无法识别jQuery的代码和美元符

<script type="text/javascript" src="js/jquery-2.1.1.js"></script>//引入位置在ajaxText.js(自定义的js代码)的上方<script type="text/javascript" src="js/ajaxTest.js"></script>

因为浏览器加载页面存在先后顺序,从head 到body,如果代码中所绑定函数的未加载或之前的方法未加载好,则调用对象或方法就会没有反应。

0 0
原创粉丝点击