什么时候使用#符号来获取DOM元素? [英] When to use the `#` symbol to get a DOM element?
本文介绍了什么时候使用#符号来获取DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有时候我需要获取这样的元素:
Sometimes I need to get elements like this:
var object = document.getElementById('ObjectName');
有时是这样的:
var object = document.getElementById('#ObjectName');
它们之间有什么区别?
推荐答案
否,您看不到
var object = document.getElementById('#ObjectName');
您没有看到,因为那意味着元素的id以#
和HTML4 ID只能以&开头字母([A-Za-z])。
You don't see that because that would mean the id of the element starts with #
and HTML4 id could only start "with a letter ([A-Za-z])".
您有时看到的是人们使用 jQuery 库,其中查询语言允许您使用
What you see is sometimes people using the jQuery library in which the query language allows you to find an object using
var elem = $('#objectId');
在将来,您会看到越来越多的人使用类似的查询语言和 querySelector 或 querySelectorAll 。
And in the future you'll see more and more people using a similar query language with querySelector or querySelectorAll.
这篇关于什么时候使用#符号来获取DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文