如何在插入表单jQuery代码的div上运行我的jquery代码?
我需要你的帮助,我的代码有问题,当从jQuery的 插入新的div插入DIV并不适用于我的jQuery代码,如果任何人有解决办法,请告诉我 这我的HTML代码如何在插入表单jQuery代码的div上运行我的jquery代码?
<input type="text" id="t" value=""/>
<a href="#" class="btn">click</a><br/>
<br/>
<div class="profileInfoSectionwall"style="border:1px solid #ccc">
text111111
<div class="remove" style="display: none;">
<a class="post_remove" rel="1" herf="#">X</a>
</div>
</div>
<br/>
<div class="profileInfoSectionwall"style="border:1px solid #ccc">
text222222
<div class="remove" style="display: none;">
<a class="post_remove" rel="2" herf="#">X</a>
</div>
</div>
和这个jQuery代码
$(document).ready(function(){
$(".profileInfoSectionwall").mouseenter(function(){
$(this).children(".remove").show();
});
$(".profileInfoSectionwall").mouseleave(function(){
$(".remove").hide();
});
$(".btn").click(function(){
var s=$("#t").attr("value");
$(this).after("<br/><br/><div class='profileInfoSectionwall' style='border:1px solid #ccc'>"+s+"<div class='remove' style='display: none;'><a class='post_remove' rel='3' herf='#'>X</a></div></div>");
return false;
})
})
在此先感谢
尝试.live
功能live in jquery
$(".profileInfoSectionwall").live('mouseenter', function(){
$(this).children(".remove").show();
});
$(".profileInfoSectionwall").live('mouseleave', function(){
$(this).children(".remove").hide();
});
您可能需要使用该方法。对这样类似尝试:
$(".profileInfoSectionwall").on('mouseenter', function(){
$(this).children(".remove").show();
});
$(".profileInfoSectionwall").on('mouseleave', function(){
$(".remove").hide();
});
感谢,但这个代码不工作与我 – 2012-03-01 12:32:03
没有阴凉,它工作正常与我:),检查你的jQuery版本 – 2012-03-01 12:39:33
我创建小提琴http://jsfiddle.net/mXBkV/1/
$(document).on('mouseenter', 'div.profileInfoSectionwall', function(){
$(this).children(".remove").show();
});
$(document).on('mouseleave', 'div.profileInfoSectionwall', function(){
$(".remove").hide();
});
事实上,你在你的情况需要的是现场()方法,你要处理的事件目前现有的或将来的DOM元素(即:由JQuery的创建) 。
因此,这里是你一个工作代码:
http://jsfiddle.net/sidou/CMab3/
让我知道如果你想要一些改进就可以了
其实曲艺答案是最好的。尽管我在我的答案中提出了.live(),但我认为最好是使用.on()方法来最佳实践和代码的稳定性,以防将来升级您的jQuery库。所以,曲艺绝对值得最好的答案。 – Sidou 2012-03-01 12:45:10
从jQuery 1.7开始,不推荐使用.live()方法。使用.on()附加事件处理程序。老版本的jQuery用户应优先使用.delegate(),而不要使用.live()。 http://api.jquery.com/live/ – arunes 2012-03-01 12:29:41
@Mona Abdelmajeed感谢这个解决方案与我一起工作,非常感谢 – 2012-03-01 12:31:23