[英]Why JQuery .html() do not work on singel div tags?
In JQuery (2.1.1. + FF 33.1) .html() do not work on singel div-Tags? 在JQuery(2.1.1。+ FF 33.1)中.html()在singel div-Tags上不起作用吗?
<head>
<title>jquery Test</title>
<script src="jquery-2.1.1.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$("#name").html( "hans" );
alert( "hans2:" + $("#name2").html() + ":");
$("#name2").html( "hans2" );
$("#name3").html( "hans3" );
$("#version").html( $().jquery );
});
</script>
JQuery-Version: :<div id="version">??</div>:
Name: :<div id="name">??</div>: <<< work
Name: :<div id="name2"/>: <<< not work
Name: :<div id="name3">??</div>:
<br>
End
</body>
The result: 结果:
JQuery-Version: :
jQuery版本:
2.1.1
2.1.1
: Name: :
: 名称: :
hans
汉斯
: Name: :
: 名称: :
hans2
汉斯2
The statement: 该声明:
$("#name2").html( "hans2" );
substitute complete html until end of file! 替换完整的html直到文件结尾!
<div id="name2"/>
is invalid HTML. <div id="name2"/>
是无效的HTML。 Use a validator . 使用验证器 。
You can only have a /
at the end of a start tag if the element is a void element. 如果元素是void元素,则只能在开始标记的末尾带有
/
。
The end tag for a div is mandatory. div的结束标记是必需的。
The div doesn't get closed until the end of the document so $("#name2").html( "hans2" );
div直到文档末尾才关闭,因此
$("#name2").html( "hans2" );
removes Name: :<div id="name3">??</div>:<br> End
from the document before you try to put content into #name3
. 删除
Name: :<div id="name3">??</div>:<br> End
在尝试将内容放入#name3
之前从文档Name: :<div id="name3">??</div>:<br> End
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.