我想找到以下内容的 id。
<div class="row" id="I_WANT_TO_FIND_THIS_CLASS">
<div class="cell100">
<div class="table">
<div class="cell40 content label bordersRight borderBottom"><input class="inputField input270" type="text" name="Source1" value=""></div>
<div class="cell15 content label bordersRight borderBottom"><input class="inputField input95" type="text" name="Date1" value=""></div>
<div class="cell45 content label borderBottom"><input class="inputField input305" type="text" name="Result1" value=""></div>
<div class="cell45 content label borderBottom"><INPUT type="button" value="x" onclick="removeRow(this)" /></div>
</div>
</div>
</div>
这是我尝试过的代码。 (最终我想删除选定的“行)”
div 的第 4 个单元格上有一个 removeRow(this)
按钮调用
function removeRow() {
//$(this).closest('.row').remove();
var div_id = $(this).closest('.row').attr('id');
alert(div_id);
}
最佳答案
您将单击的元素引用作为参数传递,因此更改方法语法以接受该参数,然后使用它来查找 row
元素
function removeRow(el) {
//$(this).closest('.row').remove();
var div_id = $(el).closest('.row').attr('id');
alert(div_id);
}
注意:由于您使用的是 jQuery,因此请使用 jQuery 事件处理程序而不是内联处理程序
关于jquery - 通过遍历class来查找id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31916437/