Jquery获取以前使用的元素的父级

时间:2012-11-23 21:01:46

标签: jquery parent

我正在创建一个完整的可编辑菜单,并遇到一个奇怪的问题。我制作了编辑菜单按钮文本的脚本,它隐藏了链接并将输入添加到div。这是代码:

$('#edit_button').click(function() {
        var temp = id;
        $("#" + temp + " a").hide("fast", function(){
            if($("#link_editor").length > 0){
                var temp_id = $("#link_editor").parent().attr("id");
                $("#link_editor").hide("fast", function(){
                    $("#link_editor").remove();
                    $("#" + temp_id + " a").show("fast");
                });
            }
            $("<input type='text' id='link_editor' value='" + $("#" + temp + " a").html() + "'>").hide().appendTo("#" + temp).show("fast");
        });
    });

菜单看起来像这样=&gt;主页博客联系 因此,当我首先点击Home,然后点击Blog等时 - 一切都很完美,但是如果我从Contacts开始并返回它就不起作用了。您可以看到实时示例here。只需将鼠标移到任何菜单按钮上,然后单击编辑图标。鼠标悬停时收到全局ID。

这是菜单代码:

<div id="sortable" class="ui-sortable">
        <div id="0"><a href="?page=Home" id="0" class="prevent_doubleclick" title="" style="display: block; ">Home</a></div>
        <div id="1"><a href="?page=Gallery" id="1" class="prevent_doubleclick" title="" style="display: block; ">Gallery</a></div>
        <div id="2"><a href="?page=Blog" id="2" class="prevent_doubleclick" title="" style="display: block; ">Blog</a></div>
        <div id="3"><a href="?page=About" id="3" class="prevent_doubleclick" title="" style="display: none; ">About</a><input type="text" id="link_editor" value="About" style=""></div>
        <div id="4"><a href="?page=Contact" id="4" class="prevent_doubleclick" title="">Contact</a></div>
    <div class="add_button" style="margin-top: -10px; display: none; "><a style="cursor: pointer; text-decoration: none;"><img src="/img/add.png">Add Button</a></div>
</div>

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了自己问题的答案。不知何故.remove()函数不等待.hide()函数完成。也许有更好的方法来解决这个问题,但我发现它只是这样: 改变了这个:

$("#link_editor").hide("fast", function(){
                    $("#link_editor").remove();
                    $("#" + temp_id + " a").show("fast");
                });

对此:

                $("#link_editor").remove();
                $("#" + temp_id + " a").show("fast");
相关问题