我的問題:爲什麼函數調用$(「#p1」)。click()不會模擬用戶點擊「p1」?即$(this).hide()根本不被執行爲什麼click()函數不起作用?
但是,如果我在$(document).ready(function(){});,註冊點擊事件,那麼$(「#p1 「)。點擊()的作品爲什麼
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$("#p1").click(function(){
$(this).hide();
});
$(document).ready(function(){
$("#p1").click(); <== this line doesn't work! why?
});
</script>
</head>
<body>
<p id="p1">If you click on me, I will disappear.</p>
</body>
</html>
謝謝。實際上這部分「$(」#p1「)。click(function(){$(this).hide();});」在另一個JavaScript文件中。那麼如何在$(document).ready回調函數中動態加載它? – user1157924 2014-10-16 15:20:52
@ user1157924:看看這個http://stackoverflow.com/a/19737116/1719752 – 2014-10-16 17:08:35