在本例中,如何从jquery/javascript获取div child?
我试图通过使用jquery的.eq()函数访问div的子元素,但是看起来好像出了什么问题,尽管事实上没有抛出错误,看起来当我对所需的子div元素执行.innerHTML时,什么也没有发生 这是我的HTML:在本例中,如何从jquery/javascript获取div child?,javascript,jquery,html,Javascript,Jquery,Html,我试图通过使用jquery的.eq()函数访问div的子元素,但是看起来好像出了什么问题,尽管事实上没有抛出错误,看起来当我对所需的子div元素执行.innerHTML时,什么也没有发生 这是我的HTML: <div id="status_container"> <div class="status_item_wrapper"> <div class="status_item_title"> <span&g
<div id="status_container">
<div class="status_item_wrapper">
<div class="status_item_title">
<span>TITLE 1</span>
</div>
<div class="status_item_content">
<table id="box-table"></table>
</div>
</div>
<div class="status_item_wrapper">
<div class="status_item_title">
<span>TITLE 2</span>
</div>
<div class="status_item_content">
<table id="box-table"></table>
</div>
</div>
</div>
我认为这将得到“TITLE 1”status\u item\u wrapper div,然后content\u wrapper将包含“status\u item\u content”对象
我应该如何从第一个父级“status\u container”到达“status\u item\u content”div
谢谢。
content\u wrapper
是一个jQuery对象,因为这是eq()
返回的对象,并且没有innerHTML
方法,所以应该使用jQuery的html()
:
content_wrapper.html( JSON.stringify(data) );
要返回本机DOM元素,可以使用get()
,并执行以下操作:
var content_wrapper = container.get(1);
content_wrapper.innerHTML = JSON.stringify(data);
编辑:
现在,您已经添加了容器
,存在一些问题eq()
将在基于零的索引的元素集合中获取某个元素,并且您使用ID作为选择器,因此不应该真正存在集合,因为ID是唯一的,并且应该只选择一个元素
如果您试图选择#status_容器
元素中的第二个子元素,您应该这样做
var content_wrapper = $("#status_container").children().eq(1);
感谢您的回复,但html()也不起作用:(查看上面我编辑的代码。不知道为什么不起作用。检查数据是否正确,但由于这是一个测试,它是一个“Hello world”字符串
var content_wrapper = $("#status_container").children().eq(1);