[英]What`s wrong with nodeValue?
Your problem was you weren't declaring your div
variable. 您的问题是您没有声明
div
变量。 I presume you must have got some error. 我想你一定有一些错误。 Just reference the
div
for which you want to change the nodeValue
and everything seems good.. Below, I've referred it with getElementsByTagName
, you can use any of other options if required. 只需引用您要为其更改
nodeValue
的div
,一切似乎都很好。.在下面,我用getElementsByTagName
引用了它,如果需要,可以使用任何其他选项。
// Javascript var div=document.getElementsByTagName('div')[0]; var textNode = div.firstChild; textNode.nodeValue = "Hello Us"; console.log(textNode.nodeValue);
<div>Hello World!</div>
I would suggest you to include an id attribute to that div since it is very likely you have other divs and that can give you trouble. 我建议您在该div中包含一个id属性,因为很可能您还有其他div,这可能会给您带来麻烦。
// html
<div id="myDivId">Hello World!</div>
// Javascript
var node = document.getElementById("myDivId");
node.textContent = "Hello Us";
if you are using jQuery, easier: 如果您使用的是jQuery,则更简单:
jQuery("#myDivId").html("Hello Us");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.