zoukankan      html  css  js  c++  java
  • 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, "> "));
    });

  • 相关阅读:
    jQuery事件篇---高级事件
    Cookie处理
    JDBC技术
    JSP行为
    JSP九大内置对象
    JSP指令学习
    Oracle数据库学习之存储过程--提高程序执行的效率
    数据库操作之游标
    PL/SQL编程接触
    数据库数据的查询----连接查询
  • 原文地址:https://www.cnblogs.com/luodengxiong/p/12391862.html
Copyright © 2011-2022 走看看