jquery - 通过遍历class来查找id

标签 jquery

我想找到以下内容的 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/

相关文章:

javascript - 当使用 $index 跟踪时,ng-init 不会第二次调用

jquery - 如何在 NodeJS 中更新 JSON 中的值并过滤以排除值?

javascript - JQuery 自动完成问题

javascript - jQuery 更改保存在变量中的 html 值

jquery datepicker 输入值格式被忽略

jQuery UI 工具提示 - 如何正确定位鼠标垂直居中?

javascript - 自动保存包含多个项目的PHP表单并使用ajax保存在mysqli中

c# - JQUERY AJAX 多个级联dropdownlist select不加载问题

jquery - 如何同时淡入和动画?

javascript - 这段代码 "obj && obj.myFunction();"在 javascript 中的作用是什么?