zoukankan      html  css  js  c++  java
  • 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)

    var html='ddfsdfsdfdsd dfsdfsdffds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=frac {-bpm sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">dfsdfsrewewrererew  ds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=frac {-bpm sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">dfsdfsdfsddfs<img _src="http://localhost:849060E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=frac {-bpm sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">ddfdsdfd<img _src="http://localhost:849060E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=frac {-bpm sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">dkjdfskjfdskl';
    
    String.prototype.replaceAll = function(s1,s2){
      return this.replace(new RegExp(s1,"gm"),s2);
    }
    
    var appendHtml="";
    var keyword="s";
    
    var partArray=html.split("<");
    $.each(partArray,function(key,value){  
       //console.log("value="+value+";key="+key); 
       var secPartArray=value.split('>');
      $.each(secPartArray,function(key1,value1){
        if(value1.indexOf("img")==0)
        {
          appendHtml+="<"+value1+">";     
        }
        else
        {
          appendHtml+=value1.replaceAll(keyword,"<strong>"+value1+"</strong>");
        }
       console.log(value1);
      });
    });
    console.log(appendHtml); 

    此思路和方法可以用来突出显示文本中的内容,img标签包含的内容可以不被错误替换。

  • 相关阅读:
    vmareworkstation 15 安装密钥
    Linux进入ftp界面退出方法
    linux安装mysql(5.1.73)
    安装http服务,用http搭建web网
    telnet远程连接
    yum出现问题解决方法
    samba
    nfs搭建
    解决VMwareworkstation无法在windows上运行
    2 shell编程
  • 原文地址:https://www.cnblogs.com/wangqiideal/p/5583716.html
Copyright © 2011-2022 走看看