Thuộc tính parentElement lấy thông tin trong JavaScript
Thuộc tính parentElement lấy thông tin trong JavaScript
Thuộc tính parentElement lấy thông tin trong JavaScript
Tự code rồi chạy thử...
Chạy Thử
<!DOCTYPE html>
<html>
<head>
<title>Thuộc tính parentElement lấy thông tin trong JavaScript</title>
</head>
<body>
<h1>Thuộc tính parentElement lấy thông tin trong JavaScript</h1>
<p>Dùng để lấy thông tin về phần tử HTML cha của phần tử HTML con, ngoài ra còn có thể thực hiện tạo thuộc tính và giá trị của thuộc tính lên phần tử cha, ở ví dụ này mình lấy thông tin tên thẻ, các thuộc tính của phần tử cha:</p>
<div id="divid" style="width:100%;float:left;height:auto">
<p id="pid">Lấy thông tin phần tử cha</p>
</div>
<p>Tên thẻ cha: <span id="tenthe"></span></p>
<p>Giá trị id thẻ cha: <span id="idthe"></span></p>
<p>Giá trị style thẻ cha: <span id="stylethe"></span></p>
<script>
// Lấy tên thẻ cha
var ten = document.getElementById("pid").parentElement.nodeName;
document.getElementById("tenthe").innerHTML = ten;
// Lấy giá trị id thẻ cha
var giatriid = document.getElementById("pid").parentElement.getAttribute("id");
document.getElementById("idthe").innerHTML = giatriid;
// Lấy giá trị style thẻ cha
var giatristyle = document.getElementById("pid").parentElement.getAttribute("style");
document.getElementById("stylethe").innerHTML = giatristyle;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Thuộc tính parentElement lấy thông tin trong JavaScript</title>
</head>
<body>
<h1>Thuộc tính parentElement lấy thông tin trong JavaScript</h1>
<p>Dùng để lấy thông tin về phần tử HTML cha của phần tử HTML con, ngoài ra còn có thể thực hiện tạo thuộc tính và giá trị của thuộc tính lên phần tử cha, ở ví dụ này mình lấy thông tin tên thẻ, các thuộc tính của phần tử cha:</p>
<div id="divid" style="width:100%;float:left;height:auto">
<p id="pid">Lấy thông tin phần tử cha</p>
</div>
<p>Tên thẻ cha: <span id="tenthe"></span></p>
<p>Giá trị id thẻ cha: <span id="idthe"></span></p>
<p>Giá trị style thẻ cha: <span id="stylethe"></span></p>
<script>
// Lấy tên thẻ cha
var ten = document.getElementById("pid").parentElement.nodeName;
document.getElementById("tenthe").innerHTML = ten;
// Lấy giá trị id thẻ cha
var giatriid = document.getElementById("pid").parentElement.getAttribute("id");
document.getElementById("idthe").innerHTML = giatriid;
// Lấy giá trị style thẻ cha
var giatristyle = document.getElementById("pid").parentElement.getAttribute("style");
document.getElementById("stylethe").innerHTML = giatristyle;
</script>
</body>
</html>
Thuộc tính parentElement lấy thông tin trong JavaScript nằm trong bài viết Thuộc tính parentElement trong JavaScript