.ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ HTMLCollection
ﯽﺷ ﮏﯾ HTMLCollection
ﯽﺷ ﮏﯾ
:ﺪﻨﮐ ﯽﻣ ﺏﺎﺨﺘﻧﺍ ﺪﻨﺳ ﮏﯾ ﺭﺩ ﺍﺭ <p>
ﺮﺻﺎﻨﻋ ﻪﻤﻫ ﺮﯾﺯ ﺪﮐ
const myCollection = document.getElementsByTagName("p");
.ﺪﻨﺘﺴﻫ ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ ﺖﺳﺮﻬﻓ ﻩﺭﺎﻤﺷ ﮏﯾ ﺎﺑ ﻪﻋﻮﻤﺠﻣ ﺭﺩ ﺩﻮﺟﻮﻣ ﺮﺻﺎﻨﻋ
ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﯼﺍﺮﺑ
myCollection[1]
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p id="demo"></p>
<script>
const myCollection = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = "The innerHTML of the second paragraph is: " + myCollection[1].innerHTML;
</script>
</body>
</html>
ﻪﺟﻮﺗ: ﺩﻮﺷ ﯽﻣ ﻉﻭﺮﺷ 0 ﺯﺍ ﺺﺧﺎﺷ.
:ﺪﻨﮐ ﯽﻣ ﺺﺨﺸﻣ ﺍﺭ HTMLCollection
ﺭﺩ ﺩﻮﺟﻮﻣ ﺮﺻﺎﻨﻋ ﺩﺍﺪﻌﺗ
myCollection.length
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p id="demo"></p>
<script>
const myCollection = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = "This document contains " + myCollection.length + " paragraphs.";
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﻪﻘﻠﺣ ﻪﻘﻠﺣ ﮏﯾ ﺭﺩ ﺍﺭ ﺮﺻﺎﻨﻋ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﻪﮐ ﺖﺳﺍ ﺪﯿﻔﻣ ﯽﻧﺎﻣﺯ
:ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ <p> ﺮﺻﺎﻨﻋ ﻪﻤﻫ ﻦﺘﻣ ﮓﻧﺭ
const myCollection = document.getElementsByTagName("p");
for (let i = 0; i < myCollection.length; i++) {
myCollection[i].style.color = "red";
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p>Click the button to change the color of all p elements.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
const myCollection = document.getElementsByTagName("p");
for (let i = 0; i < myCollection.length; i++) {
myCollection[i].style.color = "red";
}
}
</script>
</body>
</html>
ﻪﻋﻮﻤﺠﻣ ﮏﯾ HTML ﺖﺴﯿﻧ ﻪﯾﺍﺭﺁ ﮏﯾ!
ﺩﻮﺷ ﺮﻫﺎﻇ HTML ﻪﻋﻮﻤﺠﻣ ﮏﯾ ﺖﺳﺍ ﻦﮑﻤﻣ
ﺪﯿﻨﮐ ﻪﻌﺟﺍﺮﻣ ﻥﺁ ﻪﺑ ﻭ ﻩﺩﺯ ﻪﻘﻠﺣ ﺍﺭ ﺖﺴﯿﻟ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ()valueOf()، pop()، push ﺪﻨﻧﺎﻣ ﻪﯾﺍﺭﺁ ﯼﺎﻫ ﺵﻭﺭ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ ،ﻝﺎﺣ ﻦﯾﺍ