[英]jquery selector html undefined
I am trying to access the innerHtml
of a div
by id
using jQuery
. 我正在尝试使用jQuery
通过id
访问div
的innerHtml
。 It returns an object but empty text()
and undefined html()
. 它返回一个对象,但为空的text()
和未定义的html()
。
I am trying to get the 1
inside the div with id= someId
. 我正在尝试使用id = someId
在div内获取1
。
var div = document.createElement('div');
div.id = "div1";
div.innerHTML = "<div id='someId'>1</div><div>anotherdiv</div>";
alert($('#div1 #someId'));
alert($('#div1 #someId').text());
alert($('#div1 #someId').html());
The created div is not added to the DOM at all. 创建的div根本不会添加到DOM中。 After adding to the DOM .text() or .html() will work. 添加到DOM之后,.text()或.html()将起作用。
document.body.appendChild(div);
var div = document.createElement('div'); div.id = "div1"; div.innerHTML = "<div id='someId'>1</div><div>anotherdiv</div>"; document.body.appendChild(div); alert($('#div1 #someId')); alert($('#div1 #someId').text()); alert($('#div1 #someId').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.