如何获取刚刚在jquery中删除的LI项的ID?

时间:2012-07-27 23:16:40

标签: jquery html css jquery-ui

我正在尝试获取已删除的LI项的ID。以下是我的代码:

            // when the user wants to just trash a task compeletely
        $(".ui-icon-trash").on("click", function(e) {
            var id = $(this).parent("li");
            // var idVal = id.getAttribute("id");

            tb_show("Warning!", "#TB_inline?height=100&width=260&inlineId=divDelete", "");
            e.preventDefault();
            $(this).parent("li").remove();
        });

问题是id.getAttribute(“id”)返回undefined。

我的LI属性如下所示:

            <li class="ui-widget-content ui-corner-tr" id="1">
            <h5 class="ui-widget-header">Task</h5>
            <img src="graphics/task.png" width="96" height="72" />
            Sample Task
            <a href="" title="View larger image" class="ui-icon ui-icon-zoomin">View larger</a>
            <a href="" title="Delete task" class="ui-icon ui-icon-trash">Delete task</a>
        </li>

2 个答案:

答案 0 :(得分:1)

// when the user wants to just trash a task compeletely
    $(".ui-icon-trash").on("click", function(e) {
        var id = $(this).parent("li");
        // var idVal = id.getAttribute("id");

        tb_show("Warning!", "#TB_inline?height=100&width=260&inlineId=divDelete", "");
        e.preventDefault();
        var myVar = $(this).parent("li").attr("id");
        $(this).parent("li").remove();
    });

myVar应该返回已删除元素的ID

答案 1 :(得分:0)

使用jQuery,你可以这样做:

var id = $(this).parent("li").attr('id')

或者香草JS混合了jQ:

var id = $(this).parent("li")[0].id