[英]Detecting clicks inside a div using wildcards in jQuery
到目前為止,我已經使用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的類
我建議:
$('[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'));
});
JS小提琴演示 。
實際上很簡單:
$('[class^="layout"]').click(function(e) {
var parent = $(this).parent;
// do something with the parent.prop('class');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.