用于服务器端控制的 jQuery 选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13721615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:53:17  来源:igfitidea点击:

jQuery Selector for server side control

jqueryasp.net

提问by Muhammad Mutahar Alam

What is the difference between $('#<%=lblName.ClientID%>')and $("[id$=lblName]")?

$('#<%=lblName.ClientID%>')和 和有$("[id$=lblName]")什么区别?

回答by Rory McCrossan

$('#<%=lblName.ClientID%>')will find an element with the idattribute as supplied by the ClientIDproperty in ASP.Net.

$('#<%=lblName.ClientID%>')将查找具有idClientIDASP.Net 中的属性提供的属性的元素。

$("[id$=lblName]")will find an element with an idattribute that ends with lblName, for example foo-lblName.

$("[id$=lblName]")将查找具有以id结尾的属性的元素lblName,例如foo-lblName

回答by Jayendra

$('#<%=lblName.ClientID%>')- # is the Id selectorused by JQuery to identify the element with id.

$('#<%=lblName.ClientID%>')- # 是JQuery 用来标识带有 id 的元素的 Id选择器

$("[id$=lblName]")- Will select all the elements with id attribute which endswith lblName

$("[id$=lblName]")-将选择所有 id 属性以 lblName结尾的元素

回答by Viktor S.

First one($('#<%=lblName.ClientID%>')), id selector, will find an element by its ID. That is very fast as it will use native document.getElementById

第一个( $('#<%=lblName.ClientID%>')),id 选择器,将通过它的ID 找到一个元素。这非常快,因为它将使用本机document.getElementById

Second one, Attribute Ends With selector, works in different way. In IE, for instance, it will get all elements and test ID of each element if it ends with provided value (or something similar). That is much slower. In newer browsers there is querySelectorAllwhich possibly will be used to find an element by that selector, but I'm not sure if it is supported by that functions (Well, hereit is defined like vailid css3 so suppose modern browsers will support ends with selector in querySelectorAll).

第二个,Attribute Ends With selector,以不同的方式工作。例如,在 IE 中,如果它以提供的值(或类似的东西)结尾,它将获取每个元素的所有元素和测试 ID。那要慢得多。在较新的浏览器中,有querySelectorAll可能会用于通过该选择器查找元素,但我不确定该函数是否支持它(好吧,这里定义为有效的 css3,因此假设现代浏览器将支持结尾为querySelectorAll 中的选择器)。

So, in conclusion, id selector should be faster in any case and much faster in case of old browsers. At the same time, ends with selector allows you to find an element without passing its client ID to browser.

因此,总而言之, id 选择器在任何情况下都应该更快,并且在旧浏览器的情况下要快得多。同时,以选择器结尾允许您在不将其客户端 ID 传递给浏览器的情况下查找元素。

回答by Muhammad Mutahar Alam

Just adding what I came to know today, $('#<%=lblName.ClientID%>')will select only one element, however $("[id$=lblName]")will select more than one element, so if you have same id assigned to more than one element and if you would like to traverse all of them then first case will not work properly.

仅添加我今天了解到的内容,$('#<%=lblName.ClientID%>')将仅选择一个元素,但是$("[id$=lblName]")会选择多个元素,因此如果您将相同的 id 分配给多个元素,并且您想遍历所有元素,那么第一种情况将不会好好工作。