1. 指定された要素の内容を置き換えます。
$(document).ready(function(){
$("p").html("Hello <b>world</b>!");
$("p").text("你好");
$("#ksy").html("Hello <b>world</b>!");
});
違いは、html メソッドは html タグをサポートし、テキスト メソッドは文字列をサポートすることです。
2. 方法 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);
});