假设你有5个<divs>
内容,但都有一些内容。所以它就像:
<div class='module-class' id='module-1'>Hello world. <button class='delete'>X</button></div>
<div class='module-class' id='module-2'>Testing <button class='delete'>X</button></div>
<div class='module-class' id='module-3'>This is text <button class='delete'>X</button></div>
<div class='module-class' id='module-4'>Moon and Sun <button class='delete'>X</button></div>
假设您在具有<button>
的{{1}}内点击。所以,显然,它在页面中排名第3。所以你会回来“点击'模块-3'`
你会怎么做?
答案 0 :(得分:2)
$('.module-class').click(function(e){
console.log("Clicked on '" + $(this).attr('id') + "'");
});
答案 1 :(得分:0)
你也可以在没有jQuery的情况下做到这一点。
(function() {
try {
var _divs = document.getElementsByTagName('div');
} catch(e) {
console.log("_divs returned undefined");
return;
}
Array.prototype.forEach.call(_divs, function(el) {
el.addEventListener('click', function() {
console.log('div#' + this.id + ' was clicked');
});
});
})();