2011-07-08 32 views
4

我想要做這樣的事去掉「標識」:JQuery的如何從HTML內容的一部分

var html = $("#media").html(); 
$("#multimedia-content").append(html); 
$("#multimedia-content").filter("*").removeAttr("id"); 

這是第3行失敗,我想從這個部分HTML,但我不刪除所有的ID不知道如何做選擇。

謝謝!

+1

爲什麼不在第一行調用'removeAttr',或者使用'$('#multimedia-content *')。removeAttr('id');'如果這是jQuery> = 1.6,使用['.rempveProp'](http://api.jquery.com/removeProp/)。 –

回答

8

就個人而言,我會嘗試這樣的:

$('#media')       // grab the media content 
    .clone()       // make a duplicate of it 
    //.find('*')      // find all elements within the clone 
    .removeAttr('id')    // remove their ID attributes 
    //.end()       // end the .find() 
    .appendTo('#multimedia-content'); // now add it to the media container 

如果您想的#media失去其ID爲好,去除.find().end()線,否則取消註釋它們。

+1

工作例子,順便說一句:http://jsfiddle.net/BwR6b/ –

+0

你舉的例子葉ID重複的!特別是***「媒體」***。 –

+0

謝謝它的作品完美! – Santiago

0

爲了從#multimedia-content任何子元素中所有的id屬性:

$("#multimedia-content").find().each(function() { 
    $(this).removeAttr("id"); 
}); 
3

你應該這樣做:

$("#multimedia-content *").removeAttr("id"); 

這將刪除內部#multimedia-content

+0

它的作品我也喜歡它,謝謝。 – Santiago

0

嘗試元素的所有IDS這樣的:

$("*[id], #multimedia-content").removeAttr("id"); 

這樣,它只會從包含id屬性的元素id屬性。