如何按类名和类的子级使用JS innerHTML
How to use JS innerHTML by class name and child of a class
是否可以执行document.getElementByClass("post[1].classname").innerHTML?
post[1]是class='post'的第二个DIV,我想要这个的child.classname。
例如:我想获得第二个post=>div class="inner"的innerHTML。
<div class="post">Post 1
<div class="inside"></div>
</div>
<div class="post">Post 2
<div class="inside">//this here</div>
</div>
<div class="post">Post 3
<div class="inside"></div>
</div>
但如果您不想使用jQuery,但只支持以后的浏览器,则可以使用document.querySelector。http://jsfiddle.net/mendesjuan/4hEwd/
document.querySelector('.post:nth-child(2) .inside').innerHTML
以下是支持它的浏览器列表:http://caniuse.com/queryselector
尝试使用jQuery:
alert($('.post:nth-child(2) > div').html());
JSFiddle在这里。
相关文章:
- innerHtml()中的Ampersand未正确读取并在JS中进行验证
- 如何使用JS动态更新innerHTML
- 使用 innerHTML 和 vanilla JS 检索节点的内容
- JS innerHTML 中的错误值
- 确定元素是否由 JS 添加,还是原始 HTML 文档 *OR* 检测脚本何时通过 InnerHTML 更新节点
- 我是否在 JS innerHTML 中正确使用引号
- innerHTML 在 JS 中无法使用类名
- JS:避免修改“.innerHTML”
- 我用JS在下拉框中更改了一些内容,但是innerHTML不会随更改的内容一起返回
- 使用innerHTML(JS)插入select元素
- 在Native JS中向下滚动innerHTML函数
- 如何按类名和类的子级使用JS innerHTML
- 如何访问DIV's innerHTML onclick(ng-click)Angular JS中的一个按钮
- ie9 innerHTML不能与datatable .js创建的select元素一起工作
- 可点击的JS函数后PHP+Ajax innerHTML不工作
- 基本JS - innerHTML只能内联工作
- lua.vm.js innerHTML error
- JS: innerHTML会导致内容在不经意间发生改变
- JS: innerHTML inconsistency
- JS innerHTML.replace() behavior