检测图像上的点击
我正在尝试动态加载图像并显示它们,如下所示
var uploader = $('#<%=uploader.ClientId%>').plupload('getUploader');
uploader.bind('FileUploaded', function (up, file, res) {
$('#<%=thumbs.ClientId%>').append("<div id=" + file.id + ">
<a href='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "'/>
<img src='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "' width='70' height='55' data-full='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "'/></div>");
});
这是我的标记:
<div id="thumbs" class="imgContain" runat="server">
</div>
如果我在 div 上这样做,它会给我一个警报,但不是在图像上而是在 div 上:
$('.imgContain').click(function () {
alert('You Clicked Me');
});
我已经尝试使用这种方式,但它即使在 div 上也没有给我任何警报。
$('.imgContain a').click(function () {
alert('You Clicked Me');
});
那么我该怎么做呢?
I am trying to load images dynamiaclly and displaying them as shown below
var uploader = $('#<%=uploader.ClientId%>').plupload('getUploader');
uploader.bind('FileUploaded', function (up, file, res) {
$('#<%=thumbs.ClientId%>').append("<div id=" + file.id + ">
<a href='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "'/>
<img src='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "' width='70' height='55' data-full='Uploads/" + document.getElementById("<%=currentDirectory.ClientId%>").value + "/" + file.name + "'/></div>");
});
This is my markup:
<div id="thumbs" class="imgContain" runat="server">
</div>
If I do this way on the div it gives me an alert but not on the image but on the div:
$('.imgContain').click(function () {
alert('You Clicked Me');
});
And I have tried using this way but it dosent give me any alert not even on the div also.
$('.imgContain a').click(function () {
alert('You Clicked Me');
});
So how do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你应该使用
on
方法you should use the
on
method确切地知道你的 DOM 是什么样子有点困难,但我猜你是用
.imgContain
类将图像添加到 div 中的。如果你想向这些图像添加点击事件,你可以这样做:因为我相信你正在动态添加图像,所以你应该使用
.on()
方法来绑定点击事件,而不是使用.click()
。注意如果由于某种原因您使用的是以前版本的 jQuery,您可以将
.on()
更改为使用.live()
。It's a little hard to know exactly what your DOM looks like, but I guess you add the images to the div with class
.imgContain
. If you want to add a click event to those images you could do something like this:As I believe you are adding the images dynamically, you should use the
.on()
method to bind the click event, instead of using.click()
.Note If you for some reason are using a previous version of jQuery, you could change the
.on()
to use.live()
instead.对于您附加的锚点
,您需要使用
.live()
以便附加点击事件,例如:For the anchors
<a>
s that you appended, youwill need to use.live()
in order to attach to it the click event like:实时学习 jQuery 中的函数。
并在您的代码中使用它..
jQuery 版本 1.4.1 及更高版本。
因为元素是在页面加载后动态放置在 html 上的。点击功能未绑定。
Learn function live in jQuery.
and use that in your code..
jQuery version 1.4.1 and above.
since the elements are placed on the html dynamically after your page is loaded. Click function is not binded.