jquery :contains 查找指定字符串元素對象實例 並(批量)替換


http://blog.sina.com.cn/s/blog_8ee552f30101en05.html

https://www.cnblogs.com/linxixinxiang/p/11315615.html

jquery 查找指定字符串元素對象實例:

:contains(text)匹配包含給定文本的元素

HTML 代碼:

<div>   John Resig  </div>

 

<div>   George Martin  </div>

 

<div id="showContent">   Malcom John Sinclair  </div>

 

<div>   J. Ohn  </div>

jQuery 代碼:

$("div:contains('John')")


//通過Id
$("#showContent:contains('John')").html($("#showContent:contains('John')").html().replace(/John/g, "<img src=\"\" alt=\"\"/>"));

//通過class   可批量替換
$(document).ready(function(){
   $(".intro").filter(":contains('is')").each(function() {
        $(this).html($(this).html().replace("is", "are"));
   }); 
});

 

var a='12,13,14,15';現在想把字符串替換,號為-

jquery中的replace方法:a.replace(",","-");只能替換掉第一個,號。即,結果為12-13,14,15

jquery中是沒有對字符串進行replaceAll的方法,通常這個時候,全部替換采用正則表達式的方式替換。如下:

var reg = new RegExp(",","g");//g,表示全部替換。

a.replace(reg,"-");

結果:12-13-14-15

 

------------------

結合以上兩篇文章,寫成我自己的:

把頁面上的 gt; 轉成 > 需要反斜杠轉義

var reg = new RegExp(" gt\;","g");//g,表示全部替換。
$(".p_coten").filter(":contains('gt\;')").each(function() {
//console.log("contains gt\;!");
$(this).html($(this).html().replace(reg, "\> "));
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM