Can i Delete Few HTML Tags in JQuery?
In my HTML code i want to check the following HTML code and automatically delete it on page load, how do 开发者_StackOverflow中文版i do it in JQuery ?
First Code that needs to be deleted
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr class="yellow"><td width="64%" valign="top">
Second Code that needs to be deleted
</td></tr></tbody></table>
I dont want to remove whole table, want to just remove the tags
You can't remove ‘tags’ from a live HTML document. A document living in a web browser is a tree of DOM ‘nodes’. The nodes maintain the current state of the document, and were originally parsed from the tags in the HTML source. But that markup is now gone and cannot be recovered(*) or edited in the browser.
So you have a node representing the table
element, containing nodes for all the content between the <table>
start-tag and the corresponding </table>
end-tag. In this case that's one tbody
element node, which contains one tr
element node, which contains one td
element node.
To ‘unwrap’ the inner content you will need to take it out of the table and drop it in the place the table is. You can do this in jQuery using:
$('#mytableid td').eq(0).contents().replaceAll('#mytableid');
That is, replace the table with everything in its first cell.
(*: you can read innerHTML
to get a string of markup from a DOM element node, but this serialisation is newly-generated markup and won't typically be exactly the same as what you put in.)
If you put an id
or class
attribute on the table
tag, you can use the jQuery remove()
function to completely remove the element and its contents from the DOM:
$('#yourTableId').remove();
As far as I understand from your statement, you need to delete tables from page. For that,
$('table').remove();
will work. However if you give a <table id='xxx'...
then it will be more convenient to use
$('table#xxx').remove(); // or just $('xxx')..
精彩评论