Code Thuộc tính lastChild trong JavaScript
Code Thuộc tính lastChild trong JavaScript
Code Thuộc tính lastChild trong JavaScript
Tự code rồi chạy thử...
Chạy Thử
<!DOCTYPE html>
<html>
<head>
<title>Code Thuộc tính lastChild trong JavaScript</title>
</head>
<body>
<h1>Code Thuộc tính lastChild trong JavaScript</h1>
<p>Thuộc tính lastChild trong JavaScript dùng để lấy nút con cuối cùng của phần tử HTML, nút con bao gồm thẻ HTML, khoảng trắng, chú thích,..., bạn có thể hành động lên phần tử con cuối cùng.</p>
<h3>lastChild li cuối cùng</h3>
<ul id="ulid" style="border:1px solid red;padding:20px;margin-bottom:10px"><li>Trang chủ</li><li>Giới thiệu</li><li>Blog</li><li>Liên hệ</li></ul>
<div>Lấy nút con cuối cùng là thẻ ul (HTML): <span id="ulhtml"></span></div>
<h3>lastChild p cuối cùng</h3>
<div id="divp" style="border:1px solid red;padding:20px;margin-bottom:10px"><p>Xin Chào!</p><p>Cảm ơn!</p></div>
<div>Lấy nút con cuối cùng là thẻ p (HTML): <span id="phtml"></span></div>
<h3>lastChild input cuối cùng</h3>
<div id="divinput" style="border:1px solid red;padding:20px;margin-bottom:10px"><input type="text" value="1"><input type="text" value="2"></div>
<div>Lấy nút con cuối cùng là thẻ input(Value): <span id="inputhtml"></span></div>
<h3>Khoảng trắng cuối cùng</h3>
<div id="divkt" style="border:1px solid red;padding:20px;margin-bottom:10px"><input type="text" value="1"><input type="text" value="2"> </div>
<div>Lấy nút con cuối cùng là khoảng trắng(html): <span id="kthtml"></span></div>
<h3>lastChild chú thích cuối cùng</h3>
<div id="divcthich"><p>Xin Chào!</p><!--Chú thích--></div>
<div>Lấy nút con cuối cùng là chú thích (HTML): <span id="cthichhtml"></span></div>
<script>
// lastChild li cuối cùng
let valueulhtml = document.getElementById("ulid").lastChild.outerHTML;
//alert(valueulhtml);
document.getElementById("ulid").lastChild.style.color = "red";
document.getElementById("ulhtml").innerHTML = valueulhtml;
// lastChild p cuối cùng
let valuephtml = document.getElementById("divp").lastChild.innerHTML;
document.getElementById("phtml").innerHTML = valuephtml;
// lastChild input cuối cùng
let inputvalue = document.getElementById("divinput").lastChild.value;
document.getElementById("inputhtml").innerHTML = inputvalue;
// Khoảng trắng cuối cùng
let khoangtrang = document.getElementById("divkt").lastChild.nodeValue;
document.getElementById("kthtml").innerHTML = khoangtrang;
// lastChild chú thích cuối cùng
let chuthichhtml = document.getElementById("divcthich").lastChild.nodeValue;
document.getElementById("cthichhtml").innerHTML = chuthichhtml;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Code Thuộc tính lastChild trong JavaScript</title>
</head>
<body>
<h1>Code Thuộc tính lastChild trong JavaScript</h1>
<p>Thuộc tính lastChild trong JavaScript dùng để lấy nút con cuối cùng của phần tử HTML, nút con bao gồm thẻ HTML, khoảng trắng, chú thích,..., bạn có thể hành động lên phần tử con cuối cùng.</p>
<h3>lastChild li cuối cùng</h3>
<ul id="ulid" style="border:1px solid red;padding:20px;margin-bottom:10px"><li>Trang chủ</li><li>Giới thiệu</li><li>Blog</li><li>Liên hệ</li></ul>
<div>Lấy nút con cuối cùng là thẻ ul (HTML): <span id="ulhtml"></span></div>
<h3>lastChild p cuối cùng</h3>
<div id="divp" style="border:1px solid red;padding:20px;margin-bottom:10px"><p>Xin Chào!</p><p>Cảm ơn!</p></div>
<div>Lấy nút con cuối cùng là thẻ p (HTML): <span id="phtml"></span></div>
<h3>lastChild input cuối cùng</h3>
<div id="divinput" style="border:1px solid red;padding:20px;margin-bottom:10px"><input type="text" value="1"><input type="text" value="2"></div>
<div>Lấy nút con cuối cùng là thẻ input(Value): <span id="inputhtml"></span></div>
<h3>Khoảng trắng cuối cùng</h3>
<div id="divkt" style="border:1px solid red;padding:20px;margin-bottom:10px"><input type="text" value="1"><input type="text" value="2"> </div>
<div>Lấy nút con cuối cùng là khoảng trắng(html): <span id="kthtml"></span></div>
<h3>lastChild chú thích cuối cùng</h3>
<div id="divcthich"><p>Xin Chào!</p><!--Chú thích--></div>
<div>Lấy nút con cuối cùng là chú thích (HTML): <span id="cthichhtml"></span></div>
<script>
// lastChild li cuối cùng
let valueulhtml = document.getElementById("ulid").lastChild.outerHTML;
//alert(valueulhtml);
document.getElementById("ulid").lastChild.style.color = "red";
document.getElementById("ulhtml").innerHTML = valueulhtml;
// lastChild p cuối cùng
let valuephtml = document.getElementById("divp").lastChild.innerHTML;
document.getElementById("phtml").innerHTML = valuephtml;
// lastChild input cuối cùng
let inputvalue = document.getElementById("divinput").lastChild.value;
document.getElementById("inputhtml").innerHTML = inputvalue;
// Khoảng trắng cuối cùng
let khoangtrang = document.getElementById("divkt").lastChild.nodeValue;
document.getElementById("kthtml").innerHTML = khoangtrang;
// lastChild chú thích cuối cùng
let chuthichhtml = document.getElementById("divcthich").lastChild.nodeValue;
document.getElementById("cthichhtml").innerHTML = chuthichhtml;
</script>
</body>
</html>
Code Thuộc tính lastChild trong JavaScript nằm trong bài viết Thuộc tính lastChild trong JavaScript