jq替换页面字符串内容

1、替换某个指定元素的内容,比如

$(document).ready(function(){
    
    
 $("p").html("Hello <b>world</b>!");
 $("p").text("你好");
 $("#ksy").html("Hello <b>world</b>!");
});

区别是html方法是支持html标签,text方法是字符串。

2、方法二

$(document).ready(function(){
    
    
$("<span>Hello world!</span>").replaceAll("p:last"); //replaceAll方法
$("p:first").replaceWith("Hello world!"); //replaceWith方法
});

3、替换某个元素里面字符串指定的内容

//方法一
$(document).ready(function(){
    
    
var str = "要替换的字符串内容";
var newStr = str.replace(/要替换/g, "你好"); 
alert(newStr);
});
//方法二
$(document).ready(function(){
    
    
var htmls = $("#main").html();
var html_new = htmls.replace(/要替换的字符串/g, "新的字符串内容");
$('#main').html(html_new);
});

猜你喜欢

转载自blog.csdn.net/likeni1314/article/details/127585648