asp.net - $('#<%= txtFirstName.ClientID%>'). What do $ and # do in this code? - Stack Overflow

$('#<%= txtFirstName.ClientID%>').show();Trying to send ClientId to external Javascript

$('#<%= txtFirstName.ClientID%>').show();

Trying to send ClientId to external Javascript file using server tags as a parameter

<input type="text" ID="txtFirstName" runat="server" maxlength="50"
                    class="DefaultTextbox" style="width:180px;"
                    value="First Name" 
                    onfocus="ControlOnFocus('First Name',$('#<%= txtFirstName.ClientID%>').show())"
                    onblur="ControlOnBlur('First Name')"/>

function ControlOnFocus(CompareString,ControlId)
{
    alert(ControlId);

}
$('#<%= txtFirstName.ClientID%>').show();

Trying to send ClientId to external Javascript file using server tags as a parameter

<input type="text" ID="txtFirstName" runat="server" maxlength="50"
                    class="DefaultTextbox" style="width:180px;"
                    value="First Name" 
                    onfocus="ControlOnFocus('First Name',$('#<%= txtFirstName.ClientID%>').show())"
                    onblur="ControlOnBlur('First Name')"/>

function ControlOnFocus(CompareString,ControlId)
{
    alert(ControlId);

}
Share edited Dec 23, 2009 at 11:57 RickNZ 18.7k3 gold badges53 silver badges67 bronze badges asked Dec 23, 2009 at 11:04 Shantanu GuptaShantanu Gupta 21.1k56 gold badges186 silver badges293 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

$() is a short alias for the main jQuery function, also called jQuery(). You pass it a CSS selector, and in CSS, # means "the HTML element with ID...". That ID is in the ClientID variable in this program, apparently. The show() call then makes the named HTML element appear.

It seems your script uses jQuery.

$('#<%= txtFirstName.ClientID%>').show() will return the jQuery object containing the element with attribute id=<%=txtFirstName.ClientID%>, and not the ClientID value.

I guess what you really mean is this :

<input type="text" ID="txtFirstName" runat="server" maxlength="50"
                    class="DefaultTextbox" style="width:180px;"
                    value="First Name" 
                    onfocus="ControlOnFocus('First Name','<%= txtFirstName.ClientID%>')" // <= pass only the ClientID to your function
                    onblur="ControlOnBlur('First Name')"/>

function ControlOnFocus(CompareString,ControlId)
{
    $('#'+ControlId).show(); // <= do the show action here, in your function
    alert(ControlId);

}

Bud, $ is an alias for some javascript framework like jquery or prototype, the # represents an ID at the DOM. Just like CSS, those frameworks allow you to select elements using CSS syntax (. for classes, # for ids, etc). So $("#test") works almost like getElementById('test')

The jQuery syntax always begins with $, the # indicates that function show() is to be applied on the element whose Id is provided. Lets say the id is txtFirstName, then writing $('#txtFirstName') is equivalent to document.getElementById('txtDirstName') in classic Javascript. This will select the element from the Document Object Model(DOM) by the id provided. Once the element is selected, different jQuery functions can be executed on that reference. The show function makes the control/element visible. Similarly you can use .hide() to hide the element.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745046799a4608147.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信