admin管理员组文章数量:1024640
Hello I am new to coding and have a generic question, I looked everywhere and couldn't find a solution. I was following a javascript tutorial and came across this particular line of code. The childnode states that the property 'backgroundColor' is undefined and I am not sure why.
error : "Uncaught typeerror: cannot set property 'backgroundColor' of undefined"
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "sampDiv">
<p> This is a txt field </p>
<p> This is another txt field </p>
</div>
<script>
var sampDiv = document.getElementById("sampDiv");
sampDiv.childNodes[0].style.backgroundColor = "red";
</script>
</body>
</html>
Hello I am new to coding and have a generic question, I looked everywhere and couldn't find a solution. I was following a javascript tutorial and came across this particular line of code. The childnode states that the property 'backgroundColor' is undefined and I am not sure why.
error : "Uncaught typeerror: cannot set property 'backgroundColor' of undefined"
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "sampDiv">
<p> This is a txt field </p>
<p> This is another txt field </p>
</div>
<script>
var sampDiv = document.getElementById("sampDiv");
sampDiv.childNodes[0].style.backgroundColor = "red";
</script>
</body>
</html>
Share
Improve this question
edited Apr 10, 2018 at 14:01
Gianluca Fuoco
asked Apr 10, 2018 at 3:59
Gianluca FuocoGianluca Fuoco
3033 silver badges8 bronze badges
2
- 2 Please edit the exact error message into your question. – Ry- ♦ Commented Apr 10, 2018 at 4:01
-
1
The problem is
childNodes
includes the text nodes representing the whitespace between<div id="sampDiv">
and<p>
. Text nodes do not havestyle
properties – Phil Commented Apr 10, 2018 at 4:05
1 Answer
Reset to default 4Use children[0]
instead of childNodes[0]
:
https://developer.mozilla/en-US/docs/Web/API/ParentNode/children
var sampDiv = document.getElementById("sampDiv");
sampDiv.children[0].style.backgroundColor = "red";
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "sampDiv">
<p> This is a txt field </p>
<p> This is another txt field </p>
</div>
</body>
</html>
Hello I am new to coding and have a generic question, I looked everywhere and couldn't find a solution. I was following a javascript tutorial and came across this particular line of code. The childnode states that the property 'backgroundColor' is undefined and I am not sure why.
error : "Uncaught typeerror: cannot set property 'backgroundColor' of undefined"
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "sampDiv">
<p> This is a txt field </p>
<p> This is another txt field </p>
</div>
<script>
var sampDiv = document.getElementById("sampDiv");
sampDiv.childNodes[0].style.backgroundColor = "red";
</script>
</body>
</html>
Hello I am new to coding and have a generic question, I looked everywhere and couldn't find a solution. I was following a javascript tutorial and came across this particular line of code. The childnode states that the property 'backgroundColor' is undefined and I am not sure why.
error : "Uncaught typeerror: cannot set property 'backgroundColor' of undefined"
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "sampDiv">
<p> This is a txt field </p>
<p> This is another txt field </p>
</div>
<script>
var sampDiv = document.getElementById("sampDiv");
sampDiv.childNodes[0].style.backgroundColor = "red";
</script>
</body>
</html>
Share
Improve this question
edited Apr 10, 2018 at 14:01
Gianluca Fuoco
asked Apr 10, 2018 at 3:59
Gianluca FuocoGianluca Fuoco
3033 silver badges8 bronze badges
2
- 2 Please edit the exact error message into your question. – Ry- ♦ Commented Apr 10, 2018 at 4:01
-
1
The problem is
childNodes
includes the text nodes representing the whitespace between<div id="sampDiv">
and<p>
. Text nodes do not havestyle
properties – Phil Commented Apr 10, 2018 at 4:05
1 Answer
Reset to default 4Use children[0]
instead of childNodes[0]
:
https://developer.mozilla/en-US/docs/Web/API/ParentNode/children
var sampDiv = document.getElementById("sampDiv");
sampDiv.children[0].style.backgroundColor = "red";
<!doctype html>
<html>
<head>
</head>
<body>
<div id = "sampDiv">
<p> This is a txt field </p>
<p> This is another txt field </p>
</div>
</body>
</html>
本文标签: JavaScript ChildNodes Undefined type errorStack Overflow
版权声明:本文标题:JavaScript ChildNodes Undefined type error? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745588406a2157739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论