[英]jquery .click() is not registering an event listener when I start my webpage
我有一個簡單的HTML頁面和一個隨附的javascript文件。 當用戶單擊表中的圖像時,我想觸發一些JavaScript代碼。 我添加了一個jquery .click()函數,我也從document.ready中調用它,但是當我加載頁面時,無法單擊圖像。 如果我打開F12並手動編寫jquery .click(),它將啟用,並注冊事件偵聽器,然后單擊圖像以觸發代碼。 但不是僅從IntelliJ加載頁面時。
這是HTML代碼:
<html lang="en">
<head>
<h1>My page</h1>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="mainpage.css">
<script type="text/javascript" src="http_code.jquery.com_jquery-2.0.0.js"> </script>
<script type="text/javascript" src="profile.js" ></script>
</head>
<body>
<p>This is my main page</p>
<table>
<tr>
<td>
<div id="image1">
<img src="images/fluffcat.jpg" align="left"/>
</div>
</td>
<td><img src="images/midsommarcat.jpg" align="center" id="image2"/></td>
<td><img src="images/tortillacat.jpg" align="right" id="image3"/></td>
</tr>
<tr>
<td>Cat 1</td>
<td>Cat 2</td>
<td>Cat 3</td>
</tr>
</table>
</body>
</html>
這是javascript文件:
/**
* Created by jlg on 2016-06-11.
*/
function sayHello() {
console.log("Hello World")
}
function doClick() {
alert("something happened!");
window.open("Profile", "Profile", "", false);
}
function openPopup() {
console.log("I got here at least");
$("#image1").click(doClick);
}
function documentReadyFunctions() {
sayHello();
openPopup();
}
$( document ).ready(documentReadyFunctions());
我在這里想念什么? 我缺少的javascript文件中是否存在某種排序? “” console.log(“我至少到這里了”);“ 顯示在控制台以及Hello World中。
在以下代碼中,
$(document).ready(documentReadyFunctions());
documentReadyFunctions()
將立即調用該函數,並將返回值作為回調分配給ready
。
並且,由於在執行此函數時將script
加載到<head>
,因此元素在DOM中不可用。
刪除函數的()
調用。
$(document).ready(documentReadyFunctions);
// ^^^^^^^^^^^^^^^^^^^^^^ // Don't call it, pass the function reference.
這將傳遞該函數作為參考,並且在DOM准備就緒時將調用該函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.