到目前为止,我已经使用了jQuery文档
$('[class^="layout"] > ("*")').click(function(e) {
alert("inside");
});
我想要实现的是检测div中是否有某个以“layout”开头的类的内容被点击并返回该父div的类。
对于上下文,示例div类似于
<div class="builder_body" id="the_content">
<div class="layout_2cwlh_header">
<h1>Header</h1>
</div>
<div class="layout_2cwlh_wrapper">
<div class="layout_2cwlh_content">
<h1>Content</h1>
<p>sometext</p>
</div>
<div class="layout_2cwlh_sidebar">
<h1>Sidebar</h1>
</div>
</div>
</div>
所以当我点击像h1 / p之类的东西或div中的任何东西时,我需要返回父div的类
答案 0 :(得分:1)
我建议:
$('[class^="layout"]').click(function(e) {
e.stopPropagation(); // added to prevent a new alert every
// time the click bubbles to a new parent
alert($(this).closest('div[id]').attr('id'));
});
答案 1 :(得分:0)
实际上非常简单:
$('[class^="layout"]').click(function(e) {
var parent = $(this).parent;
// do something with the parent.prop('class');
});