2013-02-10 57 views
0

我有以下HTML内容:如何使用jquery克隆html内容?

<div data-test='sectionA'> 
    <input type="text" /> 
    <strong>sfdsfds</strong> 
    <p>dffdfdsf</p> 
</div> 

我需要克隆只能从上面的html内容的strongp元素。这里是我的尝试:

首先,我需要确定的部分:

if($("div").data("test") == "sectionA") 
{ 
    $(this).children().not("input").each(function() 
    { 
      alert($(this).html().clone()); 
      /* firefox says clone is not a function 
       and I'm not getting <strong>dfadfa</strong> 
       or the <p>sfdasdfsd</p> clone copy */ 

     }); 
} 
+0

,但我认为'的.html()'返回一个字符串。你只能克隆HTML元素,而不是字符串。尝试删除 – Oriol 2013-02-10 20:43:59

+0

'html'函数返回字符串而不是节点/对象,所以你没有什么可以克隆的。 – 2013-02-10 20:44:22

+0

克隆副本应该是评论的一部分 – 2013-02-10 20:44:50

回答

1
var $div = $("div[data-test=sectionA]"); 
var $strong = $('strong', $div).clone(); 
var $p = $('p', $div).clone(); 
我不习惯jQuery的