父元素id

发布于 2024-08-18 18:14:04 字数 1351 浏览 2 评论 0原文

无法获取项目拖入的列表的父 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>

can't get the parent id of the list the item is dragged into. I can get the items' id easily enough and thought i could get the parent from that but can't seem to grab it!

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

is the li dragged in. Any help much appreciated!

$("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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2024-08-25 18:14:04

编辑:

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

试试这个;)

EDIT:

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

try this;)

桃扇骨 2024-08-25 18:14:04

将 the 更改

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

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

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

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

change the

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

to

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

This avoids the nested dom parse, and adds the semicolon on the end of the statement.
EDIT:for clarity and in addition:

var parentID = $('#'+tagID).parent('ul').attr('id'); 
喜爱皱眉﹌ 2024-08-25 18:14:04

好吧——走了很长一段路...

$(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>");

    }



});

ok - gone the long way round...

$(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>");

    }



});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文