问题:querySelectorAll无法识别以数字开头的部分名称。

huangapple go评论54阅读模式
英文:

Problem querySelectorAll not recognising section name with number starting in name

问题

我正在尝试使用JavaScript(NodeJS)从HTML页面中检索特定部分:

document.querySelectorAll("section#sectionID")

它对以字母开头的部分名称正常工作。但是,如果一个部分名称以数字开头,例如:

section#4_someText_here

我尝试了多种方式来获取它。建议的方式似乎是这个:

querySelectorAll("section#\ _someText_here")

但它找不到该部分。这是否是正确的使用方式?

英文:

I am trying to retrieve a specific section from an html page using javascript (NodeJS)

document.querySelectorAll("section#sectionID")

It works fine with section names starting with letters. But one section name starts with a number like for example

section#4_someText_here

and I tried fetching it in a number of ways. The way it is suggested is I think this one

querySelectorAll("section#\ _someText_here")

But it doesn't find the section. Is this the correct way to use it?

答案1

得分: 2

你可以使用 属性选择器 来选择 id 属性

document.querySelectorAll('section[id="4_someText_here"]');
英文:

You can use the attribute selectors with the id attribute:

document.querySelectorAll('section[id="4_someText_here"]');

huangapple
  • 本文由 发表于 2023年5月10日 14:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定