2010-01-22 33 views
1
$("#id").tooltip({ 
     effect: "slide", 
     tip: '.tooltip' 
    }); 

我想刪除此代碼創建的對象。如何在運行時刪除jquery對象?

「的Flowplayer jQuery的工具」插件

這個問題在我的帖子底部描述的答案!

見底部,如果你不想失去你的時間

---------- ---------- UPDATE

這應該是這樣的

代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html><head> 
    <title>jQuery tooltip</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <script src="http://cdn.jquerytools.org/1.1.2/full/jquery.tools.min.js"></script> 

    <script type="text/javascript"> 


/******* THIS FUNCTION IS JUST FOR TEST, REMOVE IT LATER *********/ 
    $(document).ready(function() { 
     $("#_2").tooltip({ 
      effect: "slide", 
      tip: '.tooltip' , 
      position: 'bottom center' 
     }); 

    }); 
/******* THIS FUNCTION IS JUST FOR TEST, REMOVE IT LATER *********/ 




/** The code below is not working as I expect, it doesn't MOVE tooltip **/ 

      var old_id; 

    //first time - create tooltip 
     function my_create(id){ 
      $("#"+id).tooltip({ 
       effect: "slide", 
       tip: '.tooltip', 
       position: 'bottom center' 
      });  
     } 

    //next times - move tooltip to other element 
     function my_unlink(id){ 
      $("#"+id).unbind("mouseover"); 
      //todo 
     } 

     function my_link(id){ 
      //todo 
     } 


     //THE MAIN FUNCTION 

     function do_tip(new_id){ 
      if(old_id){ 
       my_unlink(old_id); 
       my_link(new_id); 
       alert(new_id); 
      } 
      else 
       my_create(new_id); 

      old_id=new_id; 
     //new_id.focus(); 
    } 

    </script> 

    <style> 
    .tooltip { 
     display: none; 
     background:transparent url(http://flowplayer.org/tools/img/tooltip/black_arrow_bottom.png); 
     font-size:14px; 
     height:70px; 
     width:160px; 
     padding:25px; 
     color:#fff; 
    } 
    h1 { 
     width: 400px; 
     text-align: center; 
     background-color: yellow; 
    } 

    </style> 
</head> 
<body> 

    <h1 onclick="do_tip(this.id)" id="_1">John</h1> 
    <h1 onclick="do_tip(this.id)" id="_2">Mary</h1> 
    <h1 onclick="do_tip(this.id)" id="_3">Dan</h1> 
    <h1 onclick="do_tip(this.id)" id="_4">Paul</h1> 
    <h1 onclick="do_tip(this.id)" id="_5">Kim</h1> 

    <div class="tooltip">There should be only one tooltip on a page!</div> 

</body></html> 

---------更新2 ----------

這裏的答案

Linking/unlinking jquery object to an element

這個一言難盡的寓意是:

我一直在想這是一個常見的,而不是代碼或特定於插件的問題。我認爲,解決方案應該是簡單的「摧毀舊的工具提示對象,然後創建一個新的,attatched到其他元素」

+0

這是你使用的插件嗎?如果是這樣的話,可能會有一種方法在插件API中將其刪除。 – Fermin 2010-01-22 13:13:48

+0

這是哪個插件?如果不知道插件創建了什麼,就無法回答。 – 2010-01-22 13:24:38

+3

請指定所有必要的html來運行此代碼,以及使用的JavaScript庫和插件 – bjelli 2010-01-22 13:24:43

回答

2

你可以編寫你想要刪除的創建的對象的代碼。

代碼的功能是首先在頁面中搜索ID爲'ID'的元素,我們稱之爲「觸發器」。

我在這裏更改代碼中的一件事:代替通過ID'ID'選擇觸發器 我選擇使用類'do_tooltip_for_this'。這樣我就可以立即爲多個觸發器設置一個工具提示。

如果用戶將鼠標移動到觸發器上,則會在頁面中顯示並定位到頁面中已存在「tooltip」類的元素,即 。 如果用戶將鼠標從觸發器上移開,該元素將自動再次隱藏。 這也適用於幾個觸發器,同樣的工具提示被重用。

您也可以通過寫

$('.tooltip').hide(); 

隱藏手工工具提示您可以通過編寫

$(".do_tooltip_for_this").unbind("mouseover"); 

這裏是整個代碼禁用整個行爲:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html><head> 

    <title>jQuery tooltip</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <script src="http://cdn.jquerytools.org/1.1.2/full/jquery.tools.min.js"></script> 

    <script type="text/javascript"> 
     var o; 
    $(document).ready(function() { 

    o = $(".do_tooltip_for_this").tooltip({ 
     effect: "slide", 
     tip: '.tooltip' , 
     position: 'bottom center' 
    }); 
            
    }); 
    </script> 
    <style> 
    .tooltip { 
     display: none; 
     background:transparent url(http://flowplayer.org/tools/img/tooltip/black_arrow_bottom.png); 
     font-size:12px; 
     height:70px; 
     width:160px; 
     padding:25px; 
     color:#fff; 
    } 
    h1 { 
     width: 400px; 
     text-align: center; 
     background-color: yellow; 
    } 

    </style> 
</head> 
<body> 

    <h1 title="a tooltip regarding John" class="do_tooltip_for_this">This is John</h1> 
    <h1 title="a tooltip regarding Paul" class="do_tooltip_for_this">This is Paul</h1> 
    <h1 title="a tooltip regarding George" class="do_tooltip_for_this">This is George</h1> 
    <h1 title="a tooltip regarding Ringo" class="do_tooltip_for_this">This is Ringo</h1> 

    <div class="tooltip"></div> 

</body></html> 
+0

謝謝,你已經理解了我最好的。現在我可以更具體地提出問題:我有5個觸發元素和1個工具提示對象。如何將工具提示從一個元素重新綁定到另一個元素? – Dan 2010-01-22 16:42:01

+0

要獲得更好的答案,您應該在您的問題中提供更多的背景。在這種情況下:不僅顯示javascript,還顯示受影響的html。 – bjelli 2010-01-23 08:46:06

+0

謝謝,我發佈了代碼! 我一直在想這是一個常見的,而不是代碼或特定於插件的問題。我認爲解決方案應該像「摧毀舊的工具提示對象,然後創建一個新的,附加到其他元素」一樣簡單。 好吧,看起來事情更復雜 – Dan 2010-01-23 11:01:59

0

如何使用這樣的:

delete (object_here); 

這將刪除JavaScript對象。通過使用jQuery

+0

什麼是「object_here」? – rahul 2010-01-22 13:24:58

+0

這實際上並不會刪除工具提示。刪除用於刪除JavaScript中的對象。 – Parrots 2010-01-22 13:39:38

0

你可以使用.remove()

$("#id").remove(); // will remove element with an id of 'id' 

EDITED

它的人here工作。它也可能對你有用。

// unbind focus and mouseover to cover all the bases just 
    // incase the tooltip is not being applied to an input 
    $("#id").unbind("focus"); 
    $("#id").unbind("mouseover"); 
+0

這就是如果我正確理解你的話...... – Reigel 2010-01-22 13:22:21

+2

這會刪除實際的項目...他想刪除插件添加到該項目的工具提示,但很難回答,不知道哪個插件。 – 2010-01-22 13:23:55

+0

我認爲這段代碼刪除了具有工具提示的對象,而不是工具提示對象。 – rahul 2010-01-22 13:24:30

0

取決於。如果你對其上的東西如.remove(),它實際上是從頁面中刪除具有該ID的整個元素。

如果您只是想從中刪除工具提示,請查看您使用的插件。通常插件提供了一個銷燬選項(由jQuery UI提供的對話框會這樣做)。

除非您願意從頁面中刪除整個元素然後重新創建它,否則核心庫中沒有這樣做。如果你想給那個方法去,這將讓你在正確的方向前進:

$('#id').replaceWith($('#id').clone()); 

默認.clone不會複製綁定到一個元素的事件和數據,使應該是一個骯髒的方式來擺脫工具提示。但是,再次看看你的插件是否有內置的清理方法。

+0

謝謝。這個插件沒有提供析構函數... – Dan 2010-01-22 16:02:03

0
$("#id").tooltip({}); 

你試過嗎?

+0

不起作用,它只是返回對象變量 – Dan 2010-01-22 16:04:03