开发者

Possible to determine if an html element is self-closed or not?

I started working on an experimental project tonight. I've realized that I need to determine if a group of selected nodes are self closing or not.

For example, suppose I query the dom and get this collection of n开发者_如何转开发odes:

<br/><br/><p></p><div></div></br/>

Is there a property on the elements that can determine which are which?

Moreover, rather than filter on specific html elements (oh, if this were the only limitations), suppose that I am parsing an XML document that can contain arbitrarily named tags.


XML does not differentiate self closing tags from empty tags, so <p /> and <p></p> are identical, as far as XML is concerned.

Some XML parsers will parse all such structures to be <p /> some will parse them all to <p></p> and some will just leave them as they are.

I would say there is no foolproof way to do this - you will have to specifically test on your browser of choice, see what exactly is returned and if you can work with that (searching for /> for example).


The only thing I can think of is for each of those nodes doing a tostring on them and checking that the last two characters are /> but as far as I know theres no predefined method of finding out whether a node is self-closed or not

I may be wrong about the tostring but there is a way to just get the entire node as text - i know theres a jQuery function for that but I cant remember how to do it in pure JS.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜