[英]Targetting server control in ASP.NET and jQuery
I am not able to find the answer in already created threads. 我无法在已经创建的线程中找到答案。 Is
是
$(<%=txtRating.ClientID%>).val("Set the value");
equivalent to 相当于
$("#<%=txtRating.ClientID%>").val("Set the value");
? ? Both are working just fine, so is there any difference?
两者都工作正常,有什么区别吗? Thank you.
谢谢。
Both work because the browser is automatically creating a variable for each tag id in order to follow HTML5 standard. 两者都起作用是因为浏览器会自动为每个标签id创建一个变量,以便遵循HTML5标准。 See http://2ality.com/2012/08/ids-are-global.html
看到http://2ality.com/2012/08/ids-are-global.html
Your code: 您的代码:
$(<%=txtRating.ClientID%>).val("Set the value");
$("#<%=txtRating.ClientID%>").val("Set the value2");
Renders this Javascript on the resulting HTML page: 在产生的HTML页面上呈现此Javascript:
$(txtRating).val("Set the value");
$("#txtRating").val("Set the value2");
The first one works because: 第一个有效的原因是:
The HTML5 standard specifies that the window object must have a property key whose value is elem if there is exactly one DOM element elem whose property id has the value key.
HTML5标准规定,如果正好有一个DOM元素elem的属性ID具有值键,则该窗口对象必须具有一个值为elem的属性键。
For example, given this HTML: 例如,给出以下HTML:
<div id="foo"></div>
You can get the HTML tag by using the id as a variable. 您可以使用id作为变量来获取HTML标记。 Hence $(foo) returns jQuery object for that tag.
因此,$(foo)返回该标签的jQuery对象。 Same as $('#foo') that also returns jQuery object for same tag.
与$('#foo')相同,它也为相同标签返回jQuery对象。
> "foo" in window
true
> foo
<div id="foo"></div>
> $(foo)
Object [ <div#foo> ]
> $('#foo')
Object [ <div#foo> ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.