如何在jquery中为动态添加的图像添加click事件? [英] How to add click event for dynamically added image in jquery?
本文介绍了如何在jquery中为动态添加的图像添加click事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< ul class = isotope items id = < span class =code-keyword> ulList >
if(Model!= null)
{
@foreach(Model.Images中的var项目)
{
< li class = item >
< a href = / Home / Album1 id = ancTag >
< h2 class = post-title > 相册1 < / h2 >
< div class = meta > < span class = count > 10张照片< / span > < / div >
< img src = @ Url.Content(item) alt = class = imgDynamic / >
< / a >
< / li >
}
}
< / ul >
我的尝试:
$( 。imgDynamic)。on(' 点击',功能(e){
调试器跨度>;
e.preventDefault();
alert(' fsdfsdf');
})
解决方案
( 。imgDynamic)。on(' click', function (e){
debugger ;
e.preventDefault( );
alert(' fsdfsdf');
})
您必须实施事件委派| jQuery学习中心 [ ^ ]
<ul class="isotope items" id="ulList">
if (Model != null)
{
@foreach (var item in Model.Images)
{
<li class="item">
<a href="/Home/Album1" id="ancTag">
<h2 class="post-title">Album 1</h2>
<div class="meta"><span class="count">10 Photos</span></div>
<img src="@Url.Content(item)" alt="" class="imgDynamic"/>
</a>
</li>
}
}
</ul>
What I have tried:
$(".imgDynamic").on('click', function (e) {
debugger;
e.preventDefault();
alert('fsdfsdf');
})
解决方案
(".imgDynamic").on('click', function (e) { debugger; e.preventDefault(); alert('fsdfsdf'); })
You have to implement Event Delegation | jQuery Learning Center[^]
这篇关于如何在jquery中为动态添加的图像添加click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文