如何将QuerySelector与数据属性和类同时使用?
我的HTML看起来像这样,
<div class="slds-col slds-size_1-of-1 slds-hide" data-controllinganswer="a1ZC2000000O2n3MAC" data-questionid="a1fC20000000VgXIAU" data-id="a1fC20000000VmzIAE" data-selectedanswer="a1ZC2000000O2zxMAC">
但是当我尝试这样做时,
this.template.querySelector('[data-id="' + e.dataset.id + '"], .slds-show')
它仍然会返回一个元素,即使没有类slds-show
。这是使用具有多个属性的QuerySelector的正确方法吗?
My html looks like this
<div class="slds-col slds-size_1-of-1 slds-hide" data-controllinganswer="a1ZC2000000O2n3MAC" data-questionid="a1fC20000000VgXIAU" data-id="a1fC20000000VmzIAE" data-selectedanswer="a1ZC2000000O2zxMAC">
But when I try to do this
this.template.querySelector('[data-id="' + e.dataset.id + '"], .slds-show')
It still returns me an element, even though there is no class as slds-show
. Is this the right way of using queryselector with multiple attributes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Comma
selector1,selector2
说具有Selector1或selector2的元素应匹配。如果您有一个space
selector1 selector2
它将匹配的元素selector2
在元素匹配selector1
内,如果您有selector1selector2
代码>没有空间,然后仅匹配一个匹配selector1
和selector2
的元素。因此,您的选择者应该是
The comma
selector1, selector2
says that elements having selector1 or selector2 should match.If you have a space
selector1 selector2
it will match an element matchingselector2
inside an element matchingselector1
, if you haveselector1selector2
without space, then it matches only an element that matches bothselector1
andselector2
.So your selector should be