2012-07-12 97 views
2
$(document).ready(function() { 

    var clone = $("#me_flash_0").clone(true); 

    if($('video').attr('class') == "pause_0"){ 
      $("#me_flash_0").remove();  
    } 

    $('.top_item img').click(function(){ 
      $("#me_flash_0_container").html(clone); 
      $(this).hide();  
    }); 

} 

#me-flash_0是一個<embed>元素。而#me_flash_0_container是包裝容器。問題是克隆的對象似乎是空的。 html(clone)沒有效果。克隆並插入jQuery

Firebug不顯示任何錯誤。

下面是相關的HTML

<div id="me_flash_0_container" class="me-plugin"> 
    <embed id="me_flash_0"> 
</div> 
<video class="pause_199" width="586px" height="440" src="some link" autoplay="true" tabindex="0" style="display: none;"></video> 

任何想法?

回答

0

a)clone是一個完整的jQuery擴展元素,而不是一個html字符串。它應該工作,如果你做.append(clone)而不是.html(clone)

b)cloneready(..)功能的本地聲明。您可能需要將其移出到全局變量中,因爲事先並不知道觸發單擊事件處理程序的時間

+0

感謝您的信息,但它不能解決問題 – 2012-07-12 16:12:20

+0

你能發佈相關的HTML嗎?另一個可能有用的解決方法是將'.attr('class')==「pause_0」'改爲'.hasClass('pause_0')',因爲即使'video'元素中有多個類,它也可以工作。另外我假設你在這裏使用''標籤。其他方面,您可能在選擇器中的「視頻」之前錯過了「#」或「。」。 – techfoobar 2012-07-12 16:15:41

+0

我發佈了相關的html。 if條件沒有問題。 – 2012-07-12 16:23:00

1
$(document).ready(function() { var clone = $("#me_flash_0").clone(true); if($('video').attr('class') == "pause_0"){ $("#me_flash_0").remove(); } $('.top_item img').click(function(){ $("#me_flash_0_container").children().remove(); $("#me_flash_0_container").append(clone); $(this).hide(); }); });

作爲@techfoobar寫道,您不能使用.html()追加一個jQuery對象。您還在JavaScript末尾丟失了一個末尾括號。

編輯:請參閱this comment上的類似問題。

0

我看到,可能會導致這種情況的問題...

  • 您正在緩存在DOM準備的對象。
    對象似乎從相關代碼開始是空的,這就是獲取緩存在變量中的內容。 (您應該添加一個關閉</embed>標籤以及)。
    我不知道什麼是應該出現在embed元素所以不知道這什麼也沒有發生的事實是預期與否的功能..
  • $('video').attr('class')將返回應用於第一video元素的類DOM。在示例代碼class_199。但你與class_0相比,所以它永遠不會匹配,也不會刪除#me_flash_0元素。