[英]Functions with alert boxes from an outside JQuery file don't work
這是我的HTML摘錄:
<html>
<head>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript" src="alert_box.js">
</script>
<title></title>
</head>
<body>
<input type="button" id="name" value="click" >
</body>
</html>
這是我的alert_box.js中的內容
$('#name').click(function(){
alert('alert box working');
});
JQuery和alert_box.js都被導入
當您致電時,DOM尚未准備就緒,因此您沒有要查找的#name
元素。 像這樣將代碼包裝在DOM中
$(document).ready(function() {
$('#name').click(function() {
alert('alert box working');
});
});
或將<script src="alert_box.js">
</body>
之前
根據給出的答案,應該解決此問題。 但我會總結一下。 您可能還想只使用托管在其他地方的遠程jquery腳本,您可以像這樣引用:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
除此以外...
<html>
<head>
//make sure your path here is correct
<script type="text/javascript" src="jquery.js"></script>
//make sure your path here is correct
<script type="text/javascript" src="alert_box.js"></script>
<title></title>
</head>
<body>
<input type="button" id="name" value="click" >
</body>
</html>
alert_box.js
(function() {
$('#name').click(function() {
alert('alert box working');
});
});
現在,在EternalHour為我提供了外部JQuery源之后,某些功能恢復了正常。 但是,這不起作用,我也不知道為什么:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="alert_box.js"></script>
<script type="text/javascript">
if(window.jQuery){ <!--this is a small test I ran ti see if jquery is loaded/-->
alert('jquery functional');
}else{
alert('jquery not loaded');
}
</script>
</head>
<body>
<input type="button" value="submit">
</body>
</html>
現在回到alert_box.js ...
$(window).ready(function(){
$(':button').click(function(){
$(this).attr('value','loading...');
});
});
該按鈕不會更改值,這是什么原因造成的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.