jquery,通过id找到控件后对其进行处理
我找到了一个需要的jquery控件,如下所示...
控件源:
<a id="ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit" class="lbEdit" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit','')">ویرایش</a>
jquery代码:
警报($('a[id$="lbEdit"]'));
之间有什么区别
ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit
我想知道和
ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit
获得
ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit
以及如何使用 jquery ?
意味着我需要检查上层 id 或任何条件,我认为对上层 id 进行硬编码不是正确的方法...
i found a required control with jquery like below...
the control source:
<a id="ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit" class="lbEdit" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit','')">ویرایش</a>
the jquery code:
alert($('a[id$="lbEdit"]'));
i want to know what is the difference between
ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit
and
ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit
and how can i get
ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit
with jquery?
mean i need to check the upper id or whatever in a condition and i think hardcode the upper id is not the correct way ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你可能想这样做。
javascript:__doPostBack('<%= lbEdit.ClientID %>','')
I think you may want to do this instead.
javascript:__doPostBack('<%= lbEdit.ClientID %>','')
ASP.Net 使用这些复杂的命名模式在
runat="server"
时生成 html 元素 id,这使得在页面上通过 id 查找控件变得困难。尝试使用此服务器标记选择器并使用 Controls
ClientID
代替:ASP.Net uses those convoluted naming patterns to generate html element ids when they're
runat="server"
that make it difficult to use to find a control by id on the page.Try using this server-tagged selector using the Controls
ClientID
instead:首先,
ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit
是元素的HTML Id属性。ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit
是元素的内部 ASP.Net 引用。它对网站的前端没有用处。在您的示例中,要使用 jQuery 选择器获取该元素的文本,您可以使用:
还值得注意的是,使用 ASP.Net Web 表单意味着您不能依赖该元素的 ID 永远相同。您必须通过它的类(您可以在 ASP.Net 中可靠地设置)及其父元素来精确定位该特定元素。
Firstly,
ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit
is the HTML Id attribute of the element.ctl00$ContentPlaceHolder1$rlvImages$ctrl0$ctrl3$lbEdit
is the internal ASP.Net reference of the element. It has no use to the front-end of the site.In your example, to get the text of that element using a jQuery selector, you would use:
It's also worth noting that using ASP.Net webforms will mean that you cannot rely on the ID of that element being that same forever. You would have to pinpoint that specific element via it's class (which you can set reliably in ASP.Net) and it's parent(s).
使用 .NET 4.0 并将 ClientIDMode 设置为 Static。
http://weblogs .asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
它将使您的 ID 更容易从 JavaScript 引用。
Use .NET 4.0 and set your CliendIDMode to Static.
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
It'll make your IDs a lot easier to reference from JavaScript.