jQuery中无法访问asp.net控件ID [英] Could not access asp.net control ID in jquery
问题描述
我有这个HTML结构:
I have this html structure:
<asp:Panel ID="divOne">
<div class="divSection1">
<asp:Panel runat="server" id="specialId" class="ClassOne ClassTwo"
<asp:Label id="MyLabel"></asp:Label>
<div id="myDiv" </div>
</asp:Panel>
</div>
</asp:Panel>
和我试图访问specialId像jQuery的$('#specialId'),$('div.specialId'),但没有成功。可有人建议吗?
And i am trying to access the "specialId" in jquery like $('#specialId'), $('div.specialId') with no success. Can someone advice?
推荐答案
我类似的回答与多一点的解释是这里
RUNAT =在asp.net服务器
补充道主的页面信息,以它的每一个控制。所以,如果你看一下DOM,您的控制会是这个样子 master_page_ctrl0_specialId
[只是一个例子。
runat="server"
in asp.net adds master and page information to each of its control. So, if you look at the DOM, your control would look something like this master_page_ctrl0_specialId
[just an example].
您有几种选择。
选项1 的:使用客户端ID - 推荐,但咩..没有这么多。我会尽量避免尽可能多写在JavaScript中的ClientID越好。
Option1: Use the client ID - recommended but meh.. not so much. I would try to avoid writing ClientID in javascript as much as possible.
$('#<%= specialId.ClientID %>')
选项2 的:使用相同的ID - 不推荐,因为它实在是太丑
Option2: Using the same ID - not recommended because its really ugly.
$('[ID $ = specialId]')
或 $('[ID * = specialId]')
以上选择器是其中的任何标识的结尾的 specialID
和任何标识 包含的 specialId
分别。
The above selectors are any id which ends with specialID
and any id which contains specialId
respectively.
选项3 的:使用类 - 强烈推荐。因为使用类选择干净,简单。另外,我看你已经使用类
,使用的CssClass
来代替.NET控件。
Option3: Using the class - highly recommended. Because selectors using classes are clean and uncomplicated. Also, I see you've used class
, use CssClass
instead for .net controls.
$('.ClassOne.ClassTwo')
选项4 的:它得到了在.NET框架4.0中引入的,在使用的ClientIDMode =静态
,控制使得其ID将保持不变。 - 推荐过
Option4: Use ClientIDMode="Static"
, which got introduced in .NET Framework 4.0, on the control so that its ID will stay unchanged. - recommended too.
<asp:Panel runat="server" ClientIDMode="Static" id="specialId" CssClass="ClassOne ClassTwo">
</asp:Panel>
这篇关于jQuery中无法访问asp.net控件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!