You can use the parentElement property in JavaScript to get the parent element of a child tag. Here's an example:

HTML:

<div id="parent">
  <p id="child">Hello</p>
</div>

JavaScript:

const childElement = document.getElementById("child");
const parentElement = childElement.parentElement;
console.log(parentElement.tagName); // Output: "DIV"

In this example, we get the child element with the ID "child" and then use the parentElement property to get its parent element. We can then use the tagName property to get the name of the parent element

Knowing the value of a child tag in HTML how can I get the name of its parent tag

原文地址: https://www.cveoy.top/t/topic/fYST 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录