Find and replace text with jquery, only text no child elements

*爱你&永不变心* 提交于 2019-12-20 01:33:09

问题


I want to find and replace text with jquery. I want to change "SKU:" to "art nu."

<span itemprop="productID" class="sku_wrapper">
   SKU: 
   <span class="sku">
      5-144
   </span>.
</span>

I tried:

$(".product_meta>.sku_wrapper:contains('SKU:')" ).text('art nu.');

but this delete the child span sku.

Hope someone has a solution...


回答1:


since jquery 1.8 you can do it also like this:

$(".sku_wrapper").html(function(i,t){
    return t.replace('SKU:','art nu.')
});



回答2:


Did you simply try

$(".sku_wrapper" ).each(function(){
    $this = $(this);
    $this.html($this.html().replace('SKU:','art nu.'));
});


来源:https://stackoverflow.com/questions/18205296/find-and-replace-text-with-jquery-only-text-no-child-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!