javascript document.getElementByTagName 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15784538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.getElementByTagName is not a function
提问by Welcome Mohlala
The code supposed to be use javascript between the <script>
tags that
consists of a mouseover event, and the list items in the HTML page must
be styled as follows: normal - black, 12, bold and over yellow, 15, bold, italic.
代码应该在<script>
由鼠标悬停事件组成的标签之间使用 javascript ,并且 HTML 页面中的列表项的样式必须如下:正常 - 黑色、12、粗体和黄色、15、粗体、斜体。
<html>
<head>
<title> Using mouseover eve </title>
<script language = "javascript">
<!--
function changeStyle() {
var item = document.getElementByTagName("li");
item.style.color = "yellow";
item.style.fontSize = "15pt";
item.style.fontWeight = "bold";
item.style.fontStyle = "italic";
}
-->
</script>
</head>
<body>
<ul style = "color: black; font-size: 12pt; font-weight: bold" >
<li onMouseOver = "changeStyle()"> item 1 </li>
<li onMouseOver = "changeStyle()"> item 2 </li>
</ul>
</body>
</html>
回答by dsgriffin
That's because the correct function name is getElementsByTagName
and notgetElementByTagName
.
那是因为正确的函数名称是getElementsByTagName
而不是getElementByTagName
。
var items = document.getElementsByTagName("li");
This will return a Nodelist of elements with that particular tag name (in this case, all list items in the document).
这将返回具有该特定标签名称的元素的 Nodelist(在本例中,文档中的所有列表项)。
Then, you could target your li's specifically as you wish, for example:
然后,您可以根据需要专门针对您的 li,例如:
items[0].style.color = "yellow"; // first li is yellow when mouseover
items[1].style.color = "red"; // second li is red when mouseover
etc.
等等。
回答by codebox
The correct name for the function is getElementsByTagName
(with an 's') and it returns an array of elements, not just a single one. If you want to get the first item from the array you will need to do somethig like this:
该函数的正确名称是getElementsByTagName
(带有一个“s”),它返回一个元素数组,而不仅仅是一个元素。如果要从数组中获取第一项,则需要执行以下操作:
var item = document.getElementsByTagName("li")[0];
回答by Nguyen Phong Thien
it should be document.getElementsByTagName
它应该是 document.getElement sByTagName