且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

$("#id") 和 $("[id=]") 的区别

更新时间:2022-05-14 08:48:15

事情是 $("#id") 总是会给你一个结果,比如 document.getelementById() 但是,当您执行 $("[id=]") 时,您会找到具有给定属性的所有元素 id,因此它会返回多个元素,因为它没有现在不要使用 javascript document.getelementById().

The thing is $("#id") will always gives you single result like document.getelementById() however when you do $("[id=]") you are finding all elements with a given attibute as id so it returns you multiple elements since it doesn't use the javascript document.getelementById() now.

$("[id=]") 是当你想从你的文档中选择一些遵循一些规则的元素时使用的东西,比如

$("[id=]") is something that you use when you want to select some elements form your document that follow some rules like

属性包含选择器 [name*="value"]

Attribute Contains Selector [name*="value"]

选择具有包含给定子字符串的值的指定属性的元素.

Selects elements that have the specified attribute with a value containing a given substring.

属性包含单词选择器 [name~="value"]

Attribute Contains Word Selector [name~="value"]

选择具有指定属性的元素,其值包含给定的单词,以空格分隔.

Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

属性以选择器结束 [name$="value"]

Attribute Ends With Selector [name$="value"]

选择具有指定属性的元素,其值恰好以给定的字符串结尾.比较区分大小写.

Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive.

等等.

有关更多信息,请参阅https://api.jquery.com/category/selectors/但是,在您的 HTML 中,您***将 id 保持为 唯一.如果您希望多个元素具有相同的 id,请改用 class.

For more information see https://api.jquery.com/category/selectors/ However in you HTML you should ideally keep the id to be unique. If you want multiple elements to have same id then use class instead.