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

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

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


当前回答

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

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

OR

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

其他回答

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

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

OR

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

jQuery的.html()可用于设置和获取匹配的非空元素(innerHTML)的内容。

var contents = $(element).html();
$(element).html("insert content into element");

只是添加一些性能见解。

几年前,我有一个项目,我们在试图将一个大的HTML / Text设置为各种HTML元素时遇到了问题。

看起来,“重新创建”元素并将其注入DOM比任何更新DOM内容的建议方法都要快得多。

比如:

Var文本= "非常大的内容"; $ (" # regTitle ") .remove (); $ (" < div id =“regTitle”>“+文字+ " < / div > ") .appendTo(“身体”); < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本>

应该能让你有更好的表现。我最近没有试着去衡量它(现在的浏览器应该很聪明),但是如果你在寻找性能,它可能会有帮助。

缺点是您需要做更多的工作来保持脚本中的DOM和引用指向正确的对象。

$("#regTitle").html("Hello World");

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

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

Or:

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