jquery的选择器不起作用……

[code="java"]

$(document).ready(function() { alert("Hello!"); }); $(".demo").click(function() { alert("I am demo"); });


点击我

[/code]

代码应该没问题吧,第一个Hello!可以正常弹出,可是I am demo 却怎么也弹不出来,怎么回事啊?

[code="java"]

$(document).ready(function() { alert("Hello!"); $(".demo").click(function() { alert("I am demo"); }); });



点击我

[/code]

把 那段代码放到 $(document).ready 就可以了;
否则因为js和html 加载和执行顺序是从上到下;当执行$(".demo")是获取不到还没有加载的 "点击我 ",, 放到ready中后是当整个页面加载完毕后执行

这是jquery1.8.0的一个bug,它过早的执行了ready函数
你的浏览器应该是IE9,或者IE10
http://bugs.jquery.com/ticket/12282
已经在1.8.1里面修复,可以后续选用1.8.1或者先回退为1.7.2也可以自己手动将jquery-1.8.0.js中的代码
[code="javascript"]
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" || ( document.readyState !== "loading" && document.addEventListener ) ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
setTimeout( jQuery.ready, 1 );

[/code]

改为:
[code="javascript"]
// Catch cases where $(document).ready() is called after the browser event has already occurred.
// IE10 and lower don't handle "interactive" properly... use a weak inference to detect it
// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
if ( document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
setTimeout( jQuery.ready, 1 );

[/code]