JQuery:自动关闭div [英] JQuery: Self-closing div
问题描述
我正在反对一个遗产系统,当div不包含任何内容时,它会产生自动关闭div。
I'm working against a legacy system which produce self-closing div when the div contains nothing.
我想使用jQuery来获取一些div的内部html,但是如果div是自动关闭的,jQuery总是会出错html。
I want use jQuery to get inner html of some div, but if the div is self-closing, jQuery always get wrong html.
请参阅下面的演示代码:
See the demo code bellow:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="application/javascript">
$(function () {
var s = $('.b').html();
alert(s);
});
</script>
</head>
<body>
<div class="a"></div>
<div class="b" />
<div class="c">This is c</div>
</html>
当我运行这段代码,我得到这个结果:
When I run this code, I get this result:
请帮助我,任何建议将不胜感激。
Please help me, any suggestions would be appreciated.
推荐答案
我怀疑这是不可能的,根据 HTML5中的自我关闭标签是否有效?以斜杠结尾的元素相当于没有的元素。
I suspect this is not possible, as per Are self-closing tags valid in HTML5? elements ending in a slash are equivalent to ones that don't.
jQuery在浏览器生成的DOM上运行,浏览器将该元素视为打开的,请参阅 this示例:
jQuery operates on the DOM the browser generates and the browser sees the element as open, see this example:
var s = $('.b').parent().html(); // <div class="a"></div><div class="b"><div class="c">This is c</div></div>
这篇关于JQuery:自动关闭div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!