我有一些文本加载到我的网站上,使用jQuery方法load()。
我想关闭(空())我点击h2时加载的div。
我的HTML:
<html>
<div class="info_item" id="post1">
<div name="x" class="item" id="loaded_with_ajax">
<h2>Click on this h2 should empty the div with class="item"</h2>
<h3>this is a h3</h3>
<p>this is a paragraph</p>
</div>
</div>
</html>
我的jQuery;
$("h2").on('click', function() {
$(this).parent("div").empty();
});
也许我的代码是错误的,但即使我在点击h2上放入一个警告(),在加载的内容上,它也没有反应。
提前致谢。
答案 0 :(得分:0)
你必须使用委托:
$(".info_item").on('click', "h2", function() {
$(this).closest(".item").empty();
});
我在这里使用closest()
来使您的代码更通用。
答案 1 :(得分:0)
根据你的问题“我想关闭(空())我点击h2时加载的div”使用jquery选择器方法,获取ID或NAME和EMPTY或者 jQuery('#parentdivID div')。html('');
答案 2 :(得分:0)
使用此
$('.item').on('click', "h2", function() {
$(this).parent().empty();
});
<强> DEMO 强>
Demo在3秒后加载ajax内容