[英]Click event not working only when used in external JS file
在外部main.js中使用時(請參見下面的內容),僅“就緒!” 文字顯示出來。 但是當以內聯方式使用而未引用main.js時(也參見下文),兩者都“就緒!” 然后“點擊!” 顯示文字。 知道我為什么想念這里嗎?
頁面內容案例1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script type='text/javascript' src='http://mywpblog.com/wp-includes/js/jquery/jquery.js?ver=1.11.2'></script>
</head>
<body>
<button class="purchase-test" type="button"></button>
<script type='text/javascript' src='http://mywpblog.com/wp-content/plugins/mytestplugin/assets/js/main.js?ver=3.0.1'></script>
</body>
</html>
頁面內容案例2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
<script type='text/javascript' src='http://mywpblog.com/wp-includes/js/jquery/jquery.js?ver=1.11.2'></script>
</head>
<body>
<button class="purchase-test" type="button"></button>
<script>
JQuery(document).ready(function() {
console.log("ready!!"); // WOKRS FINE
JQuery('.purchase-test').click(function(e) {
console.log("clicked!!"); // WOKRS FINE AS WELL
});
});
</script>
</body>
</html>
main.js
JQuery(document).ready(function() {
console.log("ready!!"); // WOKRS FINE
JQuery('.purchase-test').click(function(e) {
console.log("clicked!!"); // DOES NOT WORK
});
});
$(function() {
$('.purchase-test').on("click", function(event) {
// If '.purchase-test' is a link:
// <a href="" class="purchase-test">A Link</a>
// You must preventDefault
event.preventDefault()
// Log to console
console.log("Clicking Purchase test");
});
$('.non-purchase-test').on("click", function(event) {
console.log("Clicking Non Purchase Test");
});
$(".purchase-test-button").on("click", function(event) {
console.log("Clicking on Button");
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.