jquery, work on a control after finding it by 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:__doPo开发者_高级运维stBack('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 ...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:
$('#<%=lbEdit.ClientID %>");
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:
$("#ctl00_ContentPlaceHolder1_rlvImages_ctrl0_ctrl3_lbEdit").text();
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).
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.
I think you may want to do this instead. javascript:__doPostBack('<%= lbEdit.ClientID %>','')
精彩评论