【发布时间】:2012-03-01 19:04:26
【问题描述】:
在已附加的 div id "txt" 中附加文本。我使用了 appendChild() 函数,添加了“测试这是”但我需要“这是测试”
document.getElementById("txt").appendChild("This is");
之前
测试
之后
这是测试
帮帮我
【问题讨论】:
标签: jquery html append appendchild prepend
在已附加的 div id "txt" 中附加文本。我使用了 appendChild() 函数,添加了“测试这是”但我需要“这是测试”
document.getElementById("txt").appendChild("This is");
之前
测试
之后
这是测试
帮帮我
【问题讨论】:
标签: jquery html append appendchild prepend
您在问题的标签中找到了答案——看看.prepend() jQuery 函数。它不是在最后将子节点添加到元素,而是在开头添加它们。假设#txt是一个原本只包含文本节点'Testing'的元素,那么这段代码:
$('#txt').prepend('This is ');
会给你想要的结果。
【讨论】:
你可以这样做:
var old_string = document.getElementById("div_id").innerHTML;
var new_string = "is Testing";
document.getElementById("div_id").innerHTML = new_string + " " + old_string;
希望对您有所帮助。
【讨论】:
$("#txt").before("before text");
$("#txt").after("after text");
或者你可以从链接中受益
$("#txt").before("before ").after("after ");
【讨论】: