8000 textContent & childNodes · last-endcode/JavaScript-DOM@988779c · GitHub
[go: up one dir, main page]

Skip to content

Commit 988779c

Browse files
committed
textContent & childNodes
1 parent 45ca441 commit 988779c

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

09_textContent.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// textContent get content text
1+
// textContent for getText
2+
// same with childNodes
23

3-
const item = document.querySelector('#title');
4+
const useChildNodes = document.querySelector('h3');
5+
// only childNodes get index node-list, so use []
6+
// if not will undefined
7+
// if not use nodeValue will write like "Hello people"
8+
console.log(useChildNodes.childNodes[0].nodeValue);
49

5-
// use nodeValue
6-
// u should .nodevalue if not will use ""
7-
const node_values = item.childNodes[0].nodeValue;
8-
console.log(node_values);
9-
// output : The heading 2
10-
11-
// use textContent
12-
const getText = item.textContent;
13-
console.log(getText);
10+
// but for efisien use textContent
11+
const useTextContent = document.querySelector('h2');
12+
console.log(useTextContent.textContent);

index.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ <h3>sub title</h3>
7575
<h2>main title</h2>
7676
</div> -->
7777

78-
<!-- nextElementSibling & previousElementSibling -->
78+
<!-- nextElementSibling & previousElementSibling
7979
<h2>The Pirates</h2>
8080
<ul id="list-item">
8181
<li class="lutfy">shp</li>
8282
<li>rhp</li>
8383
<li>marine ford</li>
84-
</ul>
84+
</ul> -->
85+
86+
<!-- textContent and childNodes -->
87+
<h3>Hello people</h3>
88+
<h2>Hello World</h2>
8589
<!-- source js -->
8690
<!-- <script src="/01_getElementById.js"></script> -->
8791
<!-- <script src="/02_getElementByTagName.js"></script> -->
@@ -90,8 +94,8 @@ <h2>The Pirates</h2>
9094
<!-- <script src="/05_children.js"></script> -->
9195
<!-- <script src="/06_parentElement.js"></script> -->
9296
<!-- <script src="/07_nextSibling_previous.js"></script> -->
93-
<script src="/08_nextElementSibling_previous.js"></script>
94-
<!-- <script src="/09_textContent.js"></script> -->
97+
<!-- <script src="/08_nextElementSibling_previous.js"></script> -->
98+
<script src="/09_textContent.js"></script>
9599
<!-- <script src="/10_getAttribute.js"></script> -->
96100
</body>
97101
</html>

0 commit comments

Comments
 (0)
0