簡體   English   中英

修改內存中的Jquery元素並返回

[英]Modify in-memory Jquery element and return it

為什么此腳本不返回修改后的元素?
元素確實在函數中更改,但未正確返回。

<script>
x = "<div></div><div></div><div></div><p></p>";
function modify (x) {
    $(x).find('div').each(function(index){
        $(this).html('content text'); 
        $(this).addClass('test') ;
    }); 
    return $(x);
}
modify (x);
</script>

您的每個函數中的this都不是指x的元素。 你需要聲明一個變量來表示X元素this像這樣-

$(x).find('div').each(function(index, that){
    $(that).html('content text'); 
    $(that).addClass('test') ;
}); 

暫無
暫無

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

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