document.querySelector('.create-option:first-child')
document.querySelector('.create-option:first-child').click() does not work
有人知道为什么下面的代码不能工作吗?
document.querySelector('.create-option:first-child').click()
我试图获得类名称为"创建选项"的第一个元素,并单击它。
删除:first-child
,它将选择第一个具有该类的元素。
// just for the demo ...
[].forEach.call(document.getElementsByClassName('create-option'), function(e) {
e.addEventListener('click', function(d) {
alert(d.target.innerHTML);
})
});
document.querySelector('.create-option').click();
<div class="create-option">
a
</div>
<div class="create-option">
b
</div>
<div class="create-option">
c
</div>
<div class="create-option">
d
</div>
<div class="create-option">
e
</div>
<div class="create-option">
f
</div>
<div class="create-option">
g
</div>
<div class="create-option">
h
</div>
<div class="create-option">
i
</div>
<div class="create-option">
j
</div>
<div class="create-option">
k
</div>
.create-option:first-child
获取该类中第一个元素的第一个子元素。
要获得该类的第一个元素,省略:first-child
。
相关文章:
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- jqlite中.first()的替代方案
- jQuery last child和insetBefore不起作用
- 在控制器中访问 ng-重复$first属性
- JavaScript Get the child id
- 为什么我不能通过 document.getElementById(“node's_id”).child 选择节点子节点
- jQuery multiple class and nth-child
- Jquery Children's child element
- 在 Twig Child 中添加 CSS 和 JavaScript
- IBM Mobile First Platform (6.3) - Windows Phone 8 environmen
- 使用 JavaScript Regex 将“First.Last@example.com”变成“flast”
- Jquery的eq(0),“.first-child”,first()如果它是一个iframe,则不会抓住第一个孩子
- jQuery .first-child syntax
- 关于CSS中的first:child和last:child的问题
- IE8: element:first-child不能工作(选择我实际想要的父元素)
- document.querySelector('.create-option:first-child')
- 从tr中选择td first child不会成功
- 选择没有特定类的jQuery First Child
- jQuery -为什么在我的代码中:first和:last可以工作,而不是:n -child(2)
- 如何设置First "child"可见的树