admin管理员组文章数量:1425815
Is there a way to use jQuery to remove a single closing HTML-Tag?
Example:
<div class="one">
<div class="two">CONTENT</div>
</div>
</div>
I have to remove the last , that means the closing div-tag after div.one is closed.
Unfortunately I have to do this with JS/jQuery since the System I'm working with on this project can't remove it.
Is there a way to use jQuery to remove a single closing HTML-Tag?
Example:
<div class="one">
<div class="two">CONTENT</div>
</div>
</div>
I have to remove the last , that means the closing div-tag after div.one is closed.
Unfortunately I have to do this with JS/jQuery since the System I'm working with on this project can't remove it.
Share Improve this question asked Mar 23, 2013 at 13:38 albuveealbuvee 2,7646 gold badges29 silver badges38 bronze badges 7- Is this a string or is the content on the page? If it is content, the browser will already have parsed the HTML and create a valid DOM structure. There will be no lonely closing tag anymore. – Felix Kling Commented Mar 23, 2013 at 13:40
- Maybe you could try to make it worky using some RegExp like here stackoverflow./questions/9063333/… – m3div0 Commented Mar 23, 2013 at 13:42
- @FelixKling: It's content on the page. Therefore there is much more structure/dom around it and therefore this </div> closes other divs and the whole structure/dom falls apart… – albuvee Commented Mar 23, 2013 at 13:43
- 1 Modern browsers will try and correct these types of invalid markup. That said, it is not a job for jQuery at all. Invalid HTML should be dealt with before it reaches the clients browser at all. Is this HTML sitting in some file? – Lix Commented Mar 23, 2013 at 13:44
- @albuvee once the browser has parsed the HTML it would be very difficult to rearrange the DOM to be what you need -- why is the incorrect HTML being emitted in the first place? – Explosion Pills Commented Mar 23, 2013 at 13:46
2 Answers
Reset to default 5No, that is not possible. There simply are not closing tags at all in the page.
The closing tags exists in the HTML code before it's parsed, once it's parsed each element is one object, not a start and end point in the markup.
The browser will have tried to fix the invalid markup, but the problem is that different browsers will have done different things. Some browsers may ignore the extra closing tag, while others may imply a starting tag to make it a plete element. You could try to rearrange the elements to have them restored to the state they would have been without the extra closing tag, but to do that you need to know what all different browsers will have done when parsing the code.
Even worse, if that code is inside another div
, it would be that tag that ended there, and the invalid code would e after that. How that would be handled depends on what the code is outside the code that you have shown, and also there different browsers may handle the invalid code in different ways.
Clone metod:
$tmp = $('.one').clone( true );
$('#wrapper').empty().append($tmp)
本文标签: javascriptjQuery Remove a closing tagStack Overflow
版权声明:本文标题:javascript - jQuery: Remove a closing tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745391021a2656598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论