JQuery how to remove “id” from a part of html content

回眸只為那壹抹淺笑 提交于 2019-12-22 08:54:58

问题


I want to do something like this:

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

The 3th line fails, I want to remove all id's from this part of html but I don't know how to do the selection.

Thanks!


回答1:


Personally, i would try this:

$('#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

If you'd like #media to lose its ID as well, remove the .find() and .end() lines, otherwise un-comment them.




回答2:


You should do:

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

This will remove all the ids of the elements inside #multimedia-content




回答3:


To remove any id attributes from any child elements of #multimedia-content:

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



回答4:


Try this:

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

This way it only removes the id attribute from elements that contain the id attribute.



来源:https://stackoverflow.com/questions/6619452/jquery-how-to-remove-id-from-a-part-of-html-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!