【问题标题】:JS Custom Elements, how I can get attribute valueJS自定义元素,我如何获取属性值
【发布时间】:2021-11-21 04:49:36
【问题描述】:

我只是在学习,所以我决定重复已经存在的标签,所以

我有自定义元素custom-select,他必须做和select一样的事情,但是我需要获取属性值的值

这是我的自定义元素代码

class CustomSelect extends HTMLElement{

            static get observedAttributes(){
                return ["value"]
            }

            attributesChangeCallback(name,val){
                console.log("1234")
                if(name === "value"){
                    this.addTxt(val)
                }
            }

            connectedCallback(){



                this.innerHTML = `
                <div class = "first" id = "divId"></div>
                <div id = "spisoc">

                </div>
                `

                let elem = document.getElementById('divId')
                let elem2 = document.getElementById('spisoc')
                elem.onclick = function(){
                    elem2.classList.add("spisocDesing")
                }
                function addTxt(val){
                    elem2.innerHTML += val
                }

            }
            
        }
        customElements.define('custom-select',CustomSelect)

这是正文中的html

<body>
    <custom-select id = "elem" value = "1234">

    </custom-select>
    <script>
    </script>
</body>
</html>

【问题讨论】:

  • 试试myElement.getAttribute('attribute-name')怎么样?

标签: javascript html custom-element


【解决方案1】:

您可以按照以下脚本自定义元素

<some-element cutom-elem /> <other-element custom-elem />
<script> 
  var customs = document.querySelectorAll( "*[custom-elem]" )
</script>
<style>
    *[custom-elem] { display: block ; }
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2013-04-16
    • 2021-07-20
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多