English 中文(简体)
1. 童年 app印问题
原标题:Javascript Question regarding appendChild

Im just learning javascript and I m just wondering why this doesn t work. I ve created a button and when it is clicked I assigned a function which is supposed to append some text to all my paragraphs. I don t know why it doesn t work:

<html>
<head>
<title>javascript test</title>
</head>
<body>
<script language="javascript" type="text/javascript">

function appendStuff(){
 var node = document.getElementsByTagName("P");
 node.appendChild.createTextNode( Here s some text );

 return true;
 }

</script>
<noscript>
Your browser doesn t support javascript.
</noscript>

<input type="submit" value="click me" onclick="appendStuff();" />

<p>
This is the first paragraph.
</p>

<p>
This is the second paragraph.
</p>

<p>
This is the third paragraph.
</p>

</body>
</html>
最佳回答

你们应该通过新的节点作为理由来证明 儿童方法,例如:

var nodes = document.getElementsByTagName("P");
for(var i=0; i<nodes.length; i++) {
   nodes[i].appendChild(document.createTextNode("Here s some text"));
}
问题回答

document.getElementsByTagName returns a list (array) of element instead of just one, you have to pick up the one you d like to append (i.e. node[0])

引言

<html>
<body>
<script language="JavaScript">
function function11() {
   var myNode = document.createTextNode("New Text Node");
   document.body.appendChild(myNode);
}
</script>
<button onclick="function11();">Create text node</button>
</body>
</html>
function appendStuff(){
     var node = document.getElementsByTagName("P");
     var txt =  Here is some text ;
     var newT = document.createTextNode(txt);
    node.appendChild(newT);
     return true;
     }

above!

> <script language="javascript"
> type="text/javascript">

The language attribute has been deprecated for over a decade, it should not be used.

> function appendStuff(){   var node = document.getElementsByTagName("P");  
>   node.appendChild.createTextNode( Here s some text );
>   return true;
> }

正如其他人所指出的,getElemetsByTagName 返回:NodeList,该物质拥有长期财产,其成员可按指数查阅。 请注意,尽管它具有类似的形式,但它不是一个阵列。

A text element can be appended to the first node in the NodeList using:

  node[0].appendChild(document.createTextNode("Here s some text"));

然而,首先看node[0]是否在试图称之为其中一种方法之前就存在,这是十分安全的。

> <noscript> Your browser doesn t
> support javascript. </noscript>

浏览器显示noscript 要素必然意味着浏览器不支持 j。 对文稿内容的描述包括:

The NOSCRIPT element allows authors to provide 
alternate content when a script is not executed.

W3C, hexachloro4.01, §18.3.1

> <input type="submit" value="click me"
> onclick="appendStuff();" />

一种提交材料的形式是提交表格。 此处的类型属性较为适当的数值是“但顿”。 而XML式封闭标签是不必要的。

文件.getElementsByTagName回归阵列,而不是单.。 因此,你需要具体说明这种阵列或极有可能的独占。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签