父元素id

时间:2010-01-19 15:08:23

标签: jquery parent

无法获取项目被拖入的列表的父ID。我可以很容易地得到物品的ID,并认为我可以从中得到父母,但似乎无法抓住它!

 <li id="tag_772" class="tag tagAssigned ui-draggable" >adventurous briefs</li> 

是李被拖入。任何帮助非常感谢!

$("li.tag").draggable({ 
        connectToSortable: 'ul.assignedClass',
        helper: 'clone',
        stop: function(event, ui) { 
            projectTags('add',$(this))
        }
    });

    function projectTags(fnc, tag){
        var tagID =  $(tag).attr("id")

        var parentID = $("#"+tagID).closest('ul').attr("id"); /// closest, parent & parents doesn't work?

        $("#fdbk").prepend("<li>fnc:"+fnc+", tag:"+tagID+" < "+ $("#"+tagID).parents("ul:first").attr("id")+"</li>");

    }

HTML;

<ul id="tags_978" class="assignedClass ui-sortable">

                <li id="existingTag_1029" class="tagAssigned">space </li>  


                <li id="existingTag_1030" class="tagAssigned">light </li>  


                <li id="existingTag_1031" class="tagAssigned">continuous landscape </li>  


                <li id="existingTag_1032" class="tagAssigned">structural glass </li>
<li id="tag_772" class="tag tagAssigned ui-draggable" >adventurous briefs</li>  



        </ul>

3 个答案:

答案 0 :(得分:4)

修改

var parentID = $("#existingTag_"+tagID).parent('ul').attr("id");

试试这个;)

答案 1 :(得分:1)

更改

var tagID =  $(tag).attr("id")

var tagID =  tag.attr("id");

这避免了嵌套的dom解析,并在语句的末尾添加了分号。 编辑:为了清晰起见,另外:

var parentID = $('#'+tagID).parent('ul').attr('id'); 

答案 2 :(得分:0)

好的 - 走了很长一段路......

$(document).ready(function(){

    var selectedList;

    $("#tagBrowser").resizable({
        grid: 20,

    });

    $("#tagBrowser").draggable({ 


    });

    $("ul.assignedClass").sortable({
        revert: true,
        items: 'li.tagAssigned', 
        stop: function(event, ui) { 

        },
       sort: function(event, ui) { 
       },
       change: function(event, ui) { 
            $("#fdbk").prepend("<li>change:"+$(this).attr("id")+" </li>");
            selectedList = $(this);
       }

    });

    $("li.tag").draggable({ 
        connectToSortable: 'ul.assignedClass',
        helper: 'clone',
        stop: function(event, ui) { 
            projectTags('add',$(this))
        }
    });


    function projectTags(fnc, tag){
        var tagID =  tag.attr("id");

        var parentID =  selectedList.attr("id"); /// closest, parent & parents doesn't work?

        $("#fdbk").prepend("<li>fnc:"+fnc+", tag:"+tagID+" < "+parentID+"</li>");

    }



});