开发者

How to remove elements closed in wrong way

Can anybody answer is it possible to delete elements closed in wr开发者_JAVA技巧ong way. Example (pay attention on div.separator):

<div class="outer">
  <div class="inner">
    Content
  </div>

  <div class="separator" />

  <div class="inner">
    Content
  </div>
</div>

jQuery can't delete in standard way: jQuery('div.separator').remove();

Thanks


It's not a matter of removing elements that is closed in the wrong way, it's a matter of what the browser does with the incorrect code.

The browser will parse the incorrect code as best it can, and create elements from it. The DOM will not contain any incorrectly closed elements, the browser will always create correct elements from the code, but it's not certain what elements it actually will create.

As there is no standard for how incorrect code should be interpreted, each browser will have their own way of fixing the code so that it can create elments from it. The / can be ignored, placing the inner div inside the separator div, the browser can accept the self-closed div eventhough it's incorrect, or the tag can just be ignored completely.

As there is no single way that this code will end up as elements, there is no single way of removing the element that will work in all browsers.


Guffa is obviously correct... after a bit of testing though, Firefox (v3.6), Chrome (8.0) & IE (8.0) all behave the same way, i.e. the second inner div becomes nested in the separator div. For these three browsers at least you can:

var separator = $('div.separator');
separator.children().insertAfter(separator);
separator.remove();

Mileage on other browsers or even future versions of these browsers may vary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜