我试图在单击全键时提醒 data-keyid。
$(function() {
$('.fullkey').click( function (){
var keyId = $(this).attr('data-keyid');
alert(keyId);
});
});
<div class="fullkey">
<div data-keyid="c-key"></div>
</div>
最佳答案
Use
.find
to get the child nodes of the parent element. You can filter the set of returned elements using specific selector.$(this)
is parent selector in this example.
[data-keyid]
选择器可用于查找属性为 data-keyid
试试这个:
$(function() {
$('.fullkey').click(function() {
var keyId = $(this).find('[data-keyid]').attr('data-keyid');
alert(keyId);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="fullkey">
<div data-keyid="c-key">Click Here</div>
</div>
<div class="fullkey">
<div data-keyid="d-key">Click Here</div>
</div>
<div class="fullkey">
<div data-keyid="e-key">Click Here</div>
</div>
关于javascript - 单击事件时提醒元素属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545919/