簡體   English   中英

如何從HTML使用jQuery獲取元素

[英]How get element with jQuery from HTML

我有以下HTML代碼:

<ul>
    <li>..</li>
    <li>..</li>
    <li>  
        <a href="#" class="most_views" data-sort="most_views">
            <i class="fa fa-fw"></i> 
            Most Views
        </a>
    </li>
</ul>

我需要更新HTML行:

<i class="fa fa-fw"></i> 

與:

<i class="fa fa-fw fa-check"></i>

我的jQuery代碼是:

$('a[data-sort="most_views"]').children('i').html('<i class="fa fa-fw fa-check"></i>');

這行不通。 誰能幫我?

您需要使用addClass

$('a[data-sort="most_views"]').children('i').addClass('fa-check');

最好這樣做:

$('a[data-sort="most_views"] i').addClass('fa-check');

使用HTML()要追加的元素,雖然你做錯的方式你要追加i元素里i元素一定不要想做的事(我也不關心錯字htlmhtml )。 如果您確實要替換i元素,則需要使用:

// pretty sure you don't need this though
$('a[data-sort="most_views"]').html('<i class="fa fa-fw fa-check"></i>');

您在這里有錯字:

$('a[data-sort="most_views"]').children('i').htlm('<i class="fa fa-fw fa-check"></i>');

應該:

$('a[data-sort="most_views"]').children('i').html('<i class="fa fa-fw fa-check"></i>');

下次檢查您的html拼寫。


(開個玩笑,那不是答案):P


代替.html() ,執行以下操作:

 $('a[data-sort="most_views"]').children('i').addClass("fa-check"); 

要么

 $('.fa fa-fw')..addClass("fa-check"); 

試試吧 :

$(document).ready(function(){

    $(".fa.fa-fw").addClass("fa-check");

})

暫無
暫無

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

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