Javascript 在jQuery中使用通配符检测div内的点击
到目前为止,我已经使用jQuery文档实现了这一点Javascript 在jQuery中使用通配符检测div内的点击,javascript,jquery,Javascript,Jquery,到目前为止,我已经使用jQuery文档实现了这一点 $('[class^="layout"] > ("*")').click(function(e) { alert("inside"); }); 我试图实现的是检测一个div中是否有以“layout”开头的类被单击并返回父div的类 对于上下文,示例div如下所示 <div class="builder_body" id="the_content"> <div class="layout_2cwlh_he
$('[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的类,我建议:
$('[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'));
});
.其实很简单:
$('[class^="layout"]').click(function(e) {
var parent = $(this).parent;
// do something with the parent.prop('class');
});