简体   繁体   English

jQuery在表中找不到元素

[英]Jquery not finding element within a table

Jquery is unable to find any element within my table - here is the htmlcode. jQuery无法在我的表中找到任何元素-这是htmlcode。 if I don't specify and element name #btnLogin it attaches the click event to all the elements. 如果我未指定元素名称#btnLogin,则会将click事件附加到所有元素。 When i try to find a specific element it just doesn't pickup. 当我尝试查找特定元素时,它只是不拾取。

<script>
    $("#btnLogin").click
    (   function()
        {
            alert('Hi');
        }
    );
</script>
<table width="290px" border="1" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border-style:solid; border: 1px solid; ">
  <tr>    
    <td width="100%" align="left" valign="top" bgcolor="#FFFFFF" style="padding:5px"><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
      <tr>
        <td width="100%" bgcolor="#9B2301"  style="padding:5px"><span class="text2" style="padding:5px">Login...</span></td>
      </tr>
      <tr>
        <td style="padding-top: 8px; padding-left:10px;">
                <input name=txtUserName id="txtUserName" style="border:1px solid #CCCCCC;" type="text"  value="Username">
                <input name=txtPassword id="txtPassword" style="border:1px solid #CCCCCC;" type="text" value="Password">
        </td>
      </tr>
      <tr>
         <td valign="middle" style="padding-top: 5px; padding-left:10px;">
                <input id="Check" Type=Checkbox style=" font-size:8px;" name="Remember">&nbsp<font style="color: #9B2301; font-weight:bold">Remember Me.</font> | <font style="color: #9B2301; font-weight:bold"><a id="btnLogin" style="color: #666666; font-weight: normal;" href="#">Login</a></font> 
        </td>
      </tr>
       <tr>
         <td valign="middle" align=right style="padding-top: 5px; padding-right:15px;">
                <input id="Login" Type="Button" style="font-size:11px; border:1px solid #CCCCCC" name="Login" Value="Login">
        </td>
      </tr>
    </tr>
</table>

Any Assistance will be highly appreciated. 任何协助将不胜感激。

Thanks 谢谢

<script>
$(function() {
    $("#btnLogin").click
    (   function()
        {
            alert('Hi');
        }
    );
});
</script>

Try doing it on DOM ready instead of before the element is even generated/processed/rendered. 尝试在DOM就绪的情况下进行此操作,而不要在甚至生成/处理/渲染元素之前进行。

You are running the script before the page has loaded, therefore the element you are searching for doesn't yet exist. 您正在加载页面之前运行脚本,因此要搜索的元素尚不存在。

Try running the script when the document is ready. 准备好文档后,尝试运行脚本。

$(document).ready(function() {
    $("#btnLogin").click
    (   function()
        {
            alert('Hi');
        }
    );
});

If you are looking for #btnLogin, jquery won't be able to find it because your input button's login id is "Login". 如果您要查找#btnLogin,则jquery将无法找到它,因为输入按钮的登录ID为“ Login”。

So it should be: 所以应该是:

$("#Login").click(function() {
   alert("Hi");
});

When your function tries to attach the click handler, the element might not exist. 当您的函数尝试附加点击处理程序时,该元素可能不存在。 Try 尝试

<script>
    $(function () {
        $("#btnLogin").click(function () {
                alert('Hi');
         });
    });
</script>

The $(function () {...}); $(function(){...}); part makes jQuery wait until the page is ready and all the elements exist before finding the element with ID btnLogin and attaching the click handler. 部分使jQuery等到页面准备好并且所有元素都存在之后,再查找ID为btnLogin的元素并附加单击处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM