[英]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.