admin管理员组文章数量:1023018
Trying to create a simple document that gets the parentNode then applies a background color. My getElementByTagName
is "p" so the parentNode would be the <body>
. The background color should then be applied to the entire document, right? I'm getting an "object expected" error.
<!--purpose is to use parent node and change the background color-->
<html>
<body>
<head>
<script type="text/javascript" language="javascript">
function changeIt() {
var node;
node = document.getElementsByTagName("p").parentNode;
node.style.background-color = '#0033dd';
}
</script>
</head>
<div id="teams">
<h1>NFL Teams</h1>
<h2>NFC North</h2>
<p>Chicago Bears</p>
<p>Green Bay Packers</p>
<p>Minnesota Vikings</p>
<p>Detroit Lions</p>
<h2>NFC South</h2>
<p>New Orleans Saints</p>
<p>Atlanta Falcons</p>
<p>Carolina Panthers</p>
<p>Tampa Bay Buccannears</p>
<h2>NFC East</h2>
<p>Dallas Cowboys</p>
<p>Washington Redskins</p>
<p>Philadelphia Eagles</p>
<p>NY Giants</p>
<h2>NFC West</h2>
<p>San Francisco 49ers</p>
<p>Arizona Cardinals</p>
<p>Seattle Seahawks</p>
<p>St.Louis Rams</p>
</div>
<input type="button" value="change background color" onClick="changeIt()">
</body>
</html>
Trying to create a simple document that gets the parentNode then applies a background color. My getElementByTagName
is "p" so the parentNode would be the <body>
. The background color should then be applied to the entire document, right? I'm getting an "object expected" error.
<!--purpose is to use parent node and change the background color-->
<html>
<body>
<head>
<script type="text/javascript" language="javascript">
function changeIt() {
var node;
node = document.getElementsByTagName("p").parentNode;
node.style.background-color = '#0033dd';
}
</script>
</head>
<div id="teams">
<h1>NFL Teams</h1>
<h2>NFC North</h2>
<p>Chicago Bears</p>
<p>Green Bay Packers</p>
<p>Minnesota Vikings</p>
<p>Detroit Lions</p>
<h2>NFC South</h2>
<p>New Orleans Saints</p>
<p>Atlanta Falcons</p>
<p>Carolina Panthers</p>
<p>Tampa Bay Buccannears</p>
<h2>NFC East</h2>
<p>Dallas Cowboys</p>
<p>Washington Redskins</p>
<p>Philadelphia Eagles</p>
<p>NY Giants</p>
<h2>NFC West</h2>
<p>San Francisco 49ers</p>
<p>Arizona Cardinals</p>
<p>Seattle Seahawks</p>
<p>St.Louis Rams</p>
</div>
<input type="button" value="change background color" onClick="changeIt()">
</body>
</html>
Share
Improve this question
edited Feb 8, 2012 at 7:08
Mike Samuel
121k30 gold badges227 silver badges254 bronze badges
asked Feb 8, 2012 at 7:02
user1150101user1150101
1 Answer
Reset to default 5Background colour is a problem. It is denoted in javascript as backgroundColor
. In fact, I think all javascript notations of a css style use camels as opposed to the dash.
Also, you will need to pick a position of the getElementsByTagName array for the javascript to get the parent of it. parentNode works by getting the parent of a specific element. Not an array of elements.
function changeIt() {
var node;
node = document.getElementsByTagName("p")[0].parentNode;
node.style.backgroundColor = '#0033dd';
}
Trying to create a simple document that gets the parentNode then applies a background color. My getElementByTagName
is "p" so the parentNode would be the <body>
. The background color should then be applied to the entire document, right? I'm getting an "object expected" error.
<!--purpose is to use parent node and change the background color-->
<html>
<body>
<head>
<script type="text/javascript" language="javascript">
function changeIt() {
var node;
node = document.getElementsByTagName("p").parentNode;
node.style.background-color = '#0033dd';
}
</script>
</head>
<div id="teams">
<h1>NFL Teams</h1>
<h2>NFC North</h2>
<p>Chicago Bears</p>
<p>Green Bay Packers</p>
<p>Minnesota Vikings</p>
<p>Detroit Lions</p>
<h2>NFC South</h2>
<p>New Orleans Saints</p>
<p>Atlanta Falcons</p>
<p>Carolina Panthers</p>
<p>Tampa Bay Buccannears</p>
<h2>NFC East</h2>
<p>Dallas Cowboys</p>
<p>Washington Redskins</p>
<p>Philadelphia Eagles</p>
<p>NY Giants</p>
<h2>NFC West</h2>
<p>San Francisco 49ers</p>
<p>Arizona Cardinals</p>
<p>Seattle Seahawks</p>
<p>St.Louis Rams</p>
</div>
<input type="button" value="change background color" onClick="changeIt()">
</body>
</html>
Trying to create a simple document that gets the parentNode then applies a background color. My getElementByTagName
is "p" so the parentNode would be the <body>
. The background color should then be applied to the entire document, right? I'm getting an "object expected" error.
<!--purpose is to use parent node and change the background color-->
<html>
<body>
<head>
<script type="text/javascript" language="javascript">
function changeIt() {
var node;
node = document.getElementsByTagName("p").parentNode;
node.style.background-color = '#0033dd';
}
</script>
</head>
<div id="teams">
<h1>NFL Teams</h1>
<h2>NFC North</h2>
<p>Chicago Bears</p>
<p>Green Bay Packers</p>
<p>Minnesota Vikings</p>
<p>Detroit Lions</p>
<h2>NFC South</h2>
<p>New Orleans Saints</p>
<p>Atlanta Falcons</p>
<p>Carolina Panthers</p>
<p>Tampa Bay Buccannears</p>
<h2>NFC East</h2>
<p>Dallas Cowboys</p>
<p>Washington Redskins</p>
<p>Philadelphia Eagles</p>
<p>NY Giants</p>
<h2>NFC West</h2>
<p>San Francisco 49ers</p>
<p>Arizona Cardinals</p>
<p>Seattle Seahawks</p>
<p>St.Louis Rams</p>
</div>
<input type="button" value="change background color" onClick="changeIt()">
</body>
</html>
Share
Improve this question
edited Feb 8, 2012 at 7:08
Mike Samuel
121k30 gold badges227 silver badges254 bronze badges
asked Feb 8, 2012 at 7:02
user1150101user1150101
1 Answer
Reset to default 5Background colour is a problem. It is denoted in javascript as backgroundColor
. In fact, I think all javascript notations of a css style use camels as opposed to the dash.
Also, you will need to pick a position of the getElementsByTagName array for the javascript to get the parent of it. parentNode works by getting the parent of a specific element. Not an array of elements.
function changeIt() {
var node;
node = document.getElementsByTagName("p")[0].parentNode;
node.style.backgroundColor = '#0033dd';
}
本文标签: javascriptHow to get the parentNodeStack Overflow
版权声明:本文标题:javascript - How to get the parentNode - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745554678a2155801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论