我怎样才能做到以下几点:
document.all.regTitle.innerHTML = 'Hello World';
使用jQuery哪里regTitle是我的div id?
我怎样才能做到以下几点:
document.all.regTitle.innerHTML = 'Hello World';
使用jQuery哪里regTitle是我的div id?
当前回答
jQuery有几个函数与文本工作,如果你使用text()一个,它将为你做的工作:
$("#regTitle").text("Hello World");
此外,你可以使用html()代替,如果你有任何html标签…
其他回答
以下是你的答案:
//This is the setter of the innerHTML property in jQuery
$('#regTitle').html('Hello World');
//This is the getter of the innerHTML property in jQuery
var helloWorld = $('#regTitle').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);
});
}
var abc = document.getElementById("regTitle");
abc.innerHTML = "Hello World";
$("#regTitle").html("Hello World");
jQuery的.html()可用于设置和获取匹配的非空元素(innerHTML)的内容。
var contents = $(element).html();
$(element).html("insert content into element");