【发布时间】: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