HTML DOM Element lastChild ਪ੍ਰਤੀਯੋਗਿਤਾ

ਪਰਿਭਾਸ਼ਾ ਅਤੇ ਵਰਤੋਂ

lastChild ਪ੍ਰਤੀਯੋਗਿਤਾ ਨਿਰਧਾਰਿਤ ਨੋਡ ਦੀ ਆਖਰੀ ਸਬੰਧਤ ਨੋਡ ਨੂੰ ਵਾਪਸ ਦਿੰਦੀ ਹੈ ਜੋ Node ਆਬਜੈਕਟ ਹੈ。

lastChild ਪ੍ਰਤੀਯੋਗਿਤਾ ਰੀਡ-ਓਨਲੀ ਹੈ。

ਧਿਆਨ:

lastChild ਵਾਪਸ ਦਿੱਤਾ ਜਾਂਦਾ ਇਹ ਸਬੰਧਤ ਨੋਡ ਹੈ: ਐਲੀਮੈਂਟ ਨੋਡ, ਟੈਕਸਟ ਨੋਡ ਜਾਂ ਕਮੇਂਟ ਨੋਡ。

ਐਲੀਮੈਂਟ ਦਰਮਿਆਨ ਦੀ ਸ਼ੂਟਸ ਵੀ ਟੈਕਸਟ ਨੋਡ ਹੁੰਦੀ ਹੈ。

ਵਿਕਲਪ:

lastElementChild ਪ੍ਰਤੀਯੋਗਿਤਾ - lastElementChild ਪ੍ਰਤੀਯੋਗਿਤਾ ਆਖਰੀ ਸਬੰਧਤ ਐਲੀਮੈਂਟ ਨੂੰ ਵਾਪਸ ਦਿੰਦੀ ਹੈ (ਟੈਕਸਟ ਅਤੇ ਕਮੇਂਟ ਨੋਡ ਨੂੰ ਨਾ ਧਿਆਨ ਵਿੱਚ ਰੱਖਦੀ ਹੈ)。

ਹੋਰ ਦੇਖੋ:

childNodes ਪ੍ਰਤੀਯੋਗਿਤਾ

firstChild ਪ੍ਰਤੀਯੋਗਿਤਾ

nextSibling ਪ੍ਰਤੀਯੋਗਿਤਾ

previousSibling ਪ੍ਰਤੀਯੋਗਿਤਾ

ਨੋਡ ਪ੍ਰਤੀਯੋਗਿਤਾ

parentNode ਪ੍ਰਤੀਯੋਗਿਤਾ

nodeName ਪ੍ਰਤੀਯੋਗਿਤਾ

nodeType ਪ੍ਰਤੀਯੋਗਿਤਾ

nodeValue ਪ੍ਰਤੀਯੋਗਿਤਾ

HTML ਨੋਡ ਅਤੇ ਐਲੀਮੈਂਟ

ਵਿੱਚ HTML DOM(ਡੌਕੂਮੈਂਟ ਓਬਜੈਕਟ ਮਾਡਲ) ਵਿੱਚ,HTML ਡੌਕੂਮੈਂਟ ਸਬੰਧਤ ਨੋਡਾਂ ਦਾ ਸਮੂਹ ਹੈ ਜੋ ਸਬੰਧਤ ਨੋਡਾਂ ਨੂੰ ਰੱਖਦਾ ਹੈ (ਜਾਂ ਨਹੀਂ ਰੱਖਦਾ ਹੈ)。

ਨੋਡਇਹ ਐਲੀਮੈਂਟ ਨੋਡ, ਟੈਕਸਟ ਨੋਡ ਅਤੇ ਕਮੇਂਟ ਨੋਡ ਨੂੰ ਦੇਖਦਾ ਹੈ。

ਐਲੀਮੈਂਟਅਤੇ ਸ਼ੂਟਸ ਵਿੱਚ ਵੀ ਟੈਕਸਟ ਨੋਡ ਹੁੰਦੇ ਹਨ。

ਜਦੋਂ ਐਲੀਮੈਂਟ ਸਿਰਫ ਐਲੀਮੈਂਟ ਨੋਡ ਹੈ。

ਸਬੰਧਤ ਨੋਡ ਅਤੇ ਸਬੰਧਤ ਐਲੀਮੈਂਟ

childNodes ਵਾਪਸChild node(ਐਲੀਮੈਂਟ ਨੋਡ, ਟੈਕਸਟ ਨੋਡ ਅਤੇ ਕਮੇਂਟ ਨੋਡ)。

children ਵਾਪਸChild element(ਨਹੀਂ ਕਿ ਟੈਕਸਟ ਅਤੇ ਕਮੇਂਟ ਨੋਡ ਵਿੱਚ)。

firstChild ਅਤੇ firstElementChild

firstChild ਵਾਪਸ ਪਹਿਲਾ ਇਲੈਕਟ੍ਰੌਨChild node(Element node, text node, or comment node)。Whitespace between elements is also a text node。

firstElementChild ਵਾਪਸ ਪਹਿਲਾ ਇਲੈਕਟ੍ਰੌਨChild element(Does not return text nodes and comment nodes)。Whitespace between elements is also a text node。

lastChild ਅਤੇ lastElementChild

lastChild Return the lastChild node(Element node, text node, or comment node)。Whitespace between elements is also a text node。

lastElementChild Return the lastChild element(Does not return text nodes and comment nodes)。Whitespace between elements is also a text node。

ਇੰਸਟੈਂਸ

ਉਦਾਹਰਣ 1

Return the HTML content of the last child of the <ul> element:

document.getElementById("myList").lastChild.innerHTML;

ਆਪਣੇ ਆਪ ਨਾਲ ਪ੍ਰਯੋਗ ਕਰੋ

ਉਦਾਹਰਣ 2

Get the text of the last child of the <select> element:

let text = document.getElementById("mySelect").lastChild.text;

ਆਪਣੇ ਆਪ ਨਾਲ ਪ੍ਰਯੋਗ ਕਰੋ

ਉਦਾਹਰਣ 3

ਇਹ ਉਦਾਹਰਣ ਖਾਲੀ ਜਗ੍ਹਾਵਾਂ ਦੀ ਬੁਰੀ ਪ੍ਰਭਾਵ ਦਿਖਾਉਂਦਾ ਹੈ, ਪ੍ਰਯਤਨ ਕਰੋ ਕਿ "myDIV" ਦੇ ਆਖਰੀ ਉਪ-ਬੱਚਾ ਨੋਡ ਦਾ ਨਾਮ ਪ੍ਰਾਪਤ ਕਰੋ:

<div id="myDIV">
  <p>Looks like first child</p>
  <p>Looks like last Child</p>
</div>
<script>
let text = document.getElementById("myDIV").lastChild.nodeName;
</script>

ਆਪਣੇ ਆਪ ਨਾਲ ਪ੍ਰਯੋਗ ਕਰੋ

ਉਦਾਹਰਣ 4

ਪਰ ਜੇਕਰ ਤੁਸੀਂ ਸੋਰਸ ਵਿੱਚ ਖਾਲੀ ਜਗ੍ਹਾਵਾਂ ਹਟਾਉਂਦੇ ਹੋ, ਤਾਂ "myDIV" ਵਿੱਚ #text ਨੋਡ ਨਹੀਂ ਹੈ:

<div id="myDIV"><p>First child</p><p>Last Child</p></div>
<script>
let text = document.getElementById("myDIV").lastChild.nodeName;
</script>

ਆਪਣੇ ਆਪ ਨਾਲ ਪ੍ਰਯੋਗ ਕਰੋ

ਗਣਨਾ

element.lastChild

ਜਾਂ

node.lastChild

ਵਾਪਸ ਦਿੱਤਾ ਗਿਆ ਮੁੱਲ

ਇੰਪਰੈਸ ਵਰਣਨ
ਨੋਡ ਨੋਡ ਦਾ ਆਖਰੀ ਉਪ-ਬੱਚਾ ਨੋਡ
null ਜੇਕਰ ਕੋਈ ਉਪ-ਬੱਚਾ ਨਹੀਂ ਹੈ ਤਾਂ。

ਬਰਾਉਜ਼ਰ ਸਮਰਥਨ

element.lastChild ਇਹ DOM Level 1 (1998) ਵਿਸ਼ੇਸ਼ਤਾ ਹੈ。

ਸਾਰੇ ਬਰਾਉਜ਼ਰਾਂ ਇਸ ਨੂੰ ਪੂਰੀ ਤਰ੍ਹਾਂ ਸਮਰਥਨ ਕਰਦੇ ਹਨ:

ਚਰਮੋਸ ਆਈਈ ਐਜ਼ ਫਾਇਰਫਾਕਸ ਸੈਫਾਰੀ ਓਪਰਾ
ਚਰਮੋਸ ਆਈਈ ਐਜ਼ ਫਾਇਰਫਾਕਸ ਸੈਫਾਰੀ ਓਪਰਾ
ਸਮਰਥਨ 9-11 ਸਮਰਥਨ ਸਮਰਥਨ ਸਮਰਥਨ ਸਮਰਥਨ