英文:
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"]');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论