If it is possible to se开发者_C百科lect an asp object like asp:Label by JQuery?
Thanks a lot.
There are 3 main ways of selecting an element in jQuery.
By Id
$('#<%=lbl.ClientID%>');
By css Class
$('.className');
By attribute or tag type
$('input[name=lblName]');
Refer to this link for more ways of selecting an element How do i use jQuery selectors?
Hope this help
A coworker of mine wrote a post on how to extend jQuery to do exactly what you're wanting. The result is the ability select an asp control like this:
$(":asp(txtName)")
He accomplishes this by basically adding the following function:
jQuery.expr[':'].asp = function(elem, i, match) {
return (elem.id && elem.id.match(match[3] + "$"));
}
For a full explanation, please see his post here: Extending jQuery to Select ASP Controls
If you have the example below:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
You can access it with jquery with:
var label = $("[id$=_Label1]");
The id is the key.
Also the down-slash is optional.
Well all the asp controls will be finally rendered as HTML controls by ASP.NET.
Like asp:textbox rendered as input type text. So you can use iQuery to select them.
Yes it is. If you have label with some id. For example you can select this label with
$('#Lbl1')
If you are using MasterPage you'll probably have to append some text infront of id..
$('#ContentPlaceHolder1_Lbl1')
you can add class to the lable and access it
<asp:Label ID="Label1" runat="server" Text="Label" CssClass="lblTest"></asp:Label>
$(document).ready(function () {
$('.lblTest')///code
}
$("#<%= YourLabelID.ClientID %>")
精彩评论