我怎样才能做到以下几点:

document.all.regTitle.innerHTML = 'Hello World';

使用jQuery哪里regTitle是我的div id?


当前回答

如果你有一个jQuery对象,你想渲染而不是现有的内容:然后只是重置内容并追加新的。

var itemtoReplaceContentOf = $('#regTitle');
itemtoReplaceContentOf.html('');
newcontent.appendTo(itemtoReplaceContentOf);

Or:

$('#regTitle').empty().append(newcontent);

其他回答

你可以使用HTML或jquery的文本函数来实现它

$("#regTitle").html("hello world");

OR

$("#regTitle").text("hello world");
var abc = document.getElementById("regTitle");
abc.innerHTML = "Hello World";

如果你有一个jQuery对象,你想渲染而不是现有的内容:然后只是重置内容并追加新的。

var itemtoReplaceContentOf = $('#regTitle');
itemtoReplaceContentOf.html('');
newcontent.appendTo(itemtoReplaceContentOf);

Or:

$('#regTitle').empty().append(newcontent);

例子 $(document).ready(function() { $ (' .msg ') . html(“hello world”); }); <!DOCTYPE html > < html > < >头 < script src = " https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js " > < /脚本> > < /头 身体< > < div class = "味精" > < / div > 身体< / > < / html >

已经有答案给出了如何改变元素的内部HTML。

但是我建议,你应该使用一些像渐隐/渐隐这样的动画来改变HTML,这给了改变HTML的良好效果,而不是立即改变内部HTML。

使用动画来改变内部HTML

$('#regTitle').fadeOut(500, function() {
    $(this).html('Hello World!').fadeIn(500);
});

如果你有很多函数需要这个,那么你可以调用通用函数来改变Html内部。

function changeInnerHtml(elementPath, newText){
    $(elementPath).fadeOut(500, function() {
        $(this).html(newText).fadeIn(500);
    });
}