当我单击图像时,jQuery 单击事件不会被触发
这是我的。
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href='http://fonts.googleapis.com/css?family=Paytone+One' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Coda+Caption:800' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css' />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="@Url.Content("~/Scripts/ddpowerzoomer.js")" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) { //fire on DOM ready
$('#mainproductpicture').addpowerzoom({
defaultpower: 2,
powerrange: [2, 5],
largeimage: null,
magnifiersize: [200, 200] //<--no comma following last option!
})
})
$('#smallpictureone').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturetwo').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturethree').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturefour').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturefive').click(function () {
alert('Handler for .click() called.');
});
</script>
</head>
我的 HTML:
<div id="auctiondetails">
<img id="mainproductpicture" src="../../Content/Images/product2.JPG" alt="test" />
<img id="smallpictureone" src="../../Content/Images/product1.JPG" />
<img id="smallpicturetwo" src="../../Content/Images/product2.JPG" />
<img id="smallpicturethree" src="../../Content/Images/product3.JPG" />
<img id="smallpicturefour" src="../../Content/Images/product4.JPG" />
<img id="smallpicturefive" src="../../Content/Images/product5.JPG" />
</div>
当单击任何应该为事件连接的图像时,没有任何反应。有什么想法吗?
Here's my <head>
.
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href='http://fonts.googleapis.com/css?family=Paytone+One' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Coda+Caption:800' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css' />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="@Url.Content("~/Scripts/ddpowerzoomer.js")" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) { //fire on DOM ready
$('#mainproductpicture').addpowerzoom({
defaultpower: 2,
powerrange: [2, 5],
largeimage: null,
magnifiersize: [200, 200] //<--no comma following last option!
})
})
$('#smallpictureone').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturetwo').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturethree').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturefour').click(function () {
alert('Handler for .click() called.');
});
$('#smallpicturefive').click(function () {
alert('Handler for .click() called.');
});
</script>
</head>
And my HTML:
<div id="auctiondetails">
<img id="mainproductpicture" src="../../Content/Images/product2.JPG" alt="test" />
<img id="smallpictureone" src="../../Content/Images/product1.JPG" />
<img id="smallpicturetwo" src="../../Content/Images/product2.JPG" />
<img id="smallpicturethree" src="../../Content/Images/product3.JPG" />
<img id="smallpicturefour" src="../../Content/Images/product4.JPG" />
<img id="smallpicturefive" src="../../Content/Images/product5.JPG" />
</div>
When click any of the images that's supposed to be wired for the event, nothing happens. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将这些点击事件绑定在
DOMReady
挂钩之外,因此这些元素在该特定时间点不存在。将它们移到里面,你就会被设置:
You're binding those click events outside of your
DOMReady
hook, so those elements don't exist at that particular point in time.Move them inside, and you'll be set:
点击处理程序是在 dom 准备好之前附加的,所以它不会工作,试试这个。
The click handlers are attached before the dom is ready so it will not work, try this.
将所有 .click 绑定放入 on-ready 函数中:
jQuery(document).ready(function ($) { //fire on DOM Ready
...
`});
当选择器按 ID 搜索时,您的图像还不存在。
Put all .click bindings inside on-ready function:
jQuery(document).ready(function ($) { //fire on DOM ready
...
`});
Your images are not there yet when selector searches by id.
您只需将事件处理程序放入准备就绪的文档中即可:
You just have to place your event handlers inside document ready: