jq replace page string content

1. Replace the content of a specified element, such as

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

The difference is that the html method supports html tags, and the text method supports strings.

2. Method two

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

3. Replace the content specified by the string in an element

//方法一
$(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);
});

Guess you like

Origin blog.csdn.net/likeni1314/article/details/127585648