在 JavaScript 中,如何获取自定义 HTMLElement 的父元素(或子元素)?

huangapple go评论65阅读模式
英文:

In Javascript, how can I get the parent (or children) of a custom HTMLElement?

问题

在JavaScript中,我如何获取自定义HTMLElement的父元素(或子元素)?
我想要找出父元素的大小和位置。我还想要附加一个画布。
如果您需要更多关于这个示例问题的信息,请告诉我。我已经尝试了几种其他方法来获取父元素,但它们都失败了。我是HTML、CSS和JavaScript的新手。我主要在C++和Java中工作过。我期待您能提供的任何帮助。

以下是我尝试过的内容:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>360 Video Player Component</title>
</head>
<body>
    <!-- <script src="js/three.min.js"></script> -->
    <script type="text/javascript">
        class threeSixtyVideoElement extends HTMLElement {

            constructor() {
                super();
                const shadow = this.attachShadow({ mode: 'open' });
                console.log("hello");
                if (this.parentElement != null) {
                    console.log("parent = " + this.parentElement);
                } else {
                    console.log("Bummer");
                }
            }
        }
        window.customElements.define("threesixty-video", threeSixtyVideoElement);
    </script>
    <div class="firstLevelDiv" id="L1DIV1">
        <div class="secondLevelDiv" id="L2DIV1"> <p>class=secondLevelDiv id=L2DIV1</p> </div>
        <threesixty-video class="secondLevelDiv" id="L2DIV2"></threesixty-video>
        <div class="secondLevelDiv" id="L2DIV3"> <p>class=secondLevelDiv id=L2DIV3</p> </div>
    </div>
</body>
</html>

(Note: The code above is the same as the original code provided, and I haven't made any changes to it.)

英文:

In Javascript, how can I get the parent (or children) of a custom HTMLElement?
I want to find out the size and location of the parent element. I also want to attach a canvas.
please let me know if you want more information for this example question. I have tried several other ways to get the parent but they have all failed. I am new to HTML, CSS, an Javascript. I have mainly worked in C++ and Java. I look forward to any help you can provide.

Here is what I tried:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;360 Video Player Component&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;!-- &lt;script src=&quot;js/three.min.js&quot;&gt;&lt;/script&gt; --&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
        class threeSixtyVideoElement extends HTMLElement {

            constructor() {
                super();
                const shadow = this.attachShadow({ mode: &#39;open&#39; });
                console.log(&quot;hello&quot;);
                if (this.parentElement != null) {
                    console.log(&quot;parent = &quot; + this.parentElement);
                } else {
                    console.log(&quot;Bummer&quot;);
                }
            }
        }
        window.customElements.define(&quot;threesixty-video&quot;, threeSixtyVideoElement);
    &lt;/script&gt;
    &lt;div class=&quot;firstLevelDiv&quot; id=&quot;L1DIV1&quot;&gt;
        &lt;div class=&quot;secondLevelDiv&quot; id=&quot;L2DIV1&quot;&gt; &lt;p&gt;class=secondLevelDiv id=L2DIV1&lt;/p&gt; &lt;/div&gt;
        &lt;threesixty-video class=&quot;secondLevelDiv&quot; id=&quot;L2DIV2&quot;&gt;&lt;/threesixty-video&gt;
        &lt;div class=&quot;secondLevelDiv&quot; id=&quot;L2DIV3&quot;&gt; &lt;p&gt;class=secondLevelDiv id=L2DIV3&lt;/p&gt; &lt;/div&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

以下是翻译好的部分:

你正在尝试在构造函数中获取父元素。然而,在调用构造函数时,即对象被实例化时,父元素可能为null,因为它尚未被插入到HTML DOM中。
你需要使用connectedCallback函数,该函数在元素插入到HTML DOM中后调用,而不是在构造函数中进行检查。

connectedCallback() {
    console.log(this.parentElement);
}

此connectedCallback函数将在你的元素插入DOM中时执行,并提供父元素。

你的类应该如下所示:

class threeSixtyVideoElement extends HTMLElement {
    constructor() {
        super();
        const shadow = this.attachShadow({ mode: 'open' });
        console.log("hello");
    }
    
    connectedCallback() {
        console.log(this.parentElement)
    }
}
英文:

You are trying to get the parent element in your constructor. However, when your constructor is called i.e., the object is instantiated, the parent might be null because it has not yet been inserted in the HTML DOM.
You need to use the connectedCallback function, which is called after an element is inserted in the HTML DOM, instead of checking in the constructor.

connectedCallback() {
            console.log(this.parentElement);
        }

This connectedCallback function will execute when your element is inserted into the DOM and will give you the parent element.

Your class should look like this:

class threeSixtyVideoElement extends HTMLElement {
constructor() {
    super();
    const shadow = this.attachShadow({ mode: &#39;open&#39; });
    console.log(&quot;hello&quot;);
}

connectedCallback() {
    console.log(this.parentElement)
}
}

huangapple
  • 本文由 发表于 2023年6月5日 14:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定