2012-11-23 28 views
0

我創建了一個完整的可編輯菜單,並陷入了一個奇怪的問題。我製作了編輯菜單按鈕文本的腳本,它隱藏鏈接並將輸入附加到div。下面的代碼:jQuery獲取以前使用的元素的父親

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

菜單看起來像這樣=>主頁博客聯繫 所以,當我首先單擊主頁,然後在博客等 - 一切都運行完美,但如果我從通訊錄開始並返回它doesn沒有工作。您可以看到現場示例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> 

任何幫助,將不勝感激,謝謝。

+0

'$(這)一個.remove()父( ).find('a')。show('fast');'在回調函數中,元素可用作this,然後它只是遍歷DOM的問題? – adeneo

+0

問題是,我需要刪除先前訪問的元素,而不是目前正在構建的元素 – SinSoul

回答

0

我發現了答案,我自己的問題。不知何故.remove()函數不會等待.hide()函數完成。也許有一個更好的辦法來解決這個問題,但我發現它只能是這樣的: 改變了這個:

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

要這樣:

   $("#link_editor").remove(); 
       $("#" + temp_id + " a").show("fast");