2014-01-31 40 views
1

我想我没有这样做。为什么.on函数不能在这里工作?jQuery .on不太能够检测点击?

<html> 
    <head> 
    </head> 
    <body> 
    <button type="button" class="close" data-test="test">TEST BUTTON</button> 
    <a href="javascript:void(0);" class="close" data-test="test">TEST LINK</button> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $("document").on('test', '[data-test="test"]', function() { alert("got test!!!"); }); 
    </script> 
    </body> 
</html> 
+0

你的HTML是无效的,而且也没有'test'事件。 – j08691

+1

我想你忘了如何JavaScript ...没有'测试'事件。 HTML中也没有''标签。 –

+0

更改你的html结构,就像在我的回答中一样! –

回答

1

更改为:

$(document).on('click', '[data-test="test"]', function() { 
    alert("got test!!!"); 
}); 
+0

不,$(文档)和$(窗口)都没有任何影响。 – user3179047

+0

@ user3179047现在它应该可以工作。 –

0

由于这是按钮没有被动态地添加的最简单的解决方案将是

$(".close").click (function() { alert("got test!!!"); }); 
0

试试这个:

jQuery(document).ready(function($){ 
    $(document).on('click', '[data-test="test"]', function() { alert("got test!!!"); }); 

}); 

DEMO:http://jsfiddle.net/uSQg6/

改变你的HTML结构如下:

<!DOCTYPE html> 
<html> 
    <head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
     jQuery(document).ready(function($){ 
     $(document).on('click', '[data-test="test"]', function() { alert("got test!!!"); }); 

    }); 
    </script> 
    </head> 
    <body> 
    <button type="button" class="close" data-test="test">TEST BUTTON</button> 
    <a href="javascript:void(0);" class="close" data-test="test">TEST LINK</button> 

    </body> 
</html> 
+0

你不需要在文档准备好和头脑中做到这一点。 – theshadowmonkey