簡體   English   中英

jQuery-通過ID選擇一個元素不起作用

[英]jQuery - Select an element by ID not working

我試圖選擇一個元素,然后添加/刪除一個類。 我已經用其他元素完成了很多次,但是對於這個元素,它並不想工作。

我的HTML如下:

<div>
 <a class="login-headerText" id="hypLogin" style="padding-right:5px; float:left" >Login</a>
 <h4 class="login-headerText" style="font-weight:bold; padding-right:5px; float:left">/</h4>
 <a class="login-headerText"  id="hypCreateAccount">Create Account</a>
</div>   

div包裝在另一個div中。 (ID =模態)

我想選擇“ hypLogin”,然后單擊添加一個類。 如果我在onClick事件中執行此操作,則它將起作用,但是在單獨的腳本中將不起作用。 (該腳本已被引用並可以用於其他元素)

這是我的jQuery

$('#hypLogin').click(function () {
    $(this).removeClass('login-headerText').addClass('login-headerText-Unselected');
});

嘗試調試它,它一點都沒有被擊中。

可能真的很簡單。

幾件事情:

  1. 您必須執行以下操作:

     $( document ).ready(function() { }); 
  2. 這些元素是否動態打印?

  3. 嘗試使用:

     $('#hypLogin').on('click', function () { }); 
  4. 嘗試將您的代碼放在模式打開事件上。

這是工作提琴

嘗試這個:

$(document).ready(function () {
    $('#hypLogin').click(function () {

        $(this).removeClass('login-headerText').addClass('login-headerText-Unselected');
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM