javascript - 如何从我的组件的 ShadowDOM 元素中获取属性
问题描述
如何获取 ShadowDOM 中输入元素的“已检查”属性值。
class BitBoxComponent extends HTMLElement {
static get observedAttributes() {
return ["checked"];
}
constructor() {
super();
this.inputElem = document.createElement("INPUT");
this.inputElem.setAttribute("type", "checkbox");
this.inputElem.setAttribute("checked", false);
// this.inputElem.style.border = "solid 1px grey";
const style = document.createElement("style");
style.textContent = ``;
const shadow = this.attachShadow({
mode: "open"
});
shadow.appendChild(style);
shadow.appendChild(this.inputElem);
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) return;
var isTrueSet = newValue == "true";
this.inputElem.checked = isTrueSet;
}
}
customElements.define("bit-box", BitBoxComponent);
document.addEventListener('DOMContentLoaded', () => {
const bit = document.getElementById("bit-7").getAttribute("checked");
console.log(bit);
})
<bit-box id="bit-7"></bit-box>
<bit-box id="bit-6"></bit-box>
<bit-box id="bit-0"></bit-box>
<!-- https://github.com/mateusortiz/webcomponents-the-right-way -->
单击复选框后,“已选中”属性的值应更改。然后我想获得使用它的价值。
解决方案
首先,当您使用时,mode: 'open'
无需存储引用 - 它自动在this.shadowRoot
(也从外部)可用。
接下来,创建一个 getter 和 setter for checked
,它们checked
的行为就像一个真正的布尔属性:
class BitBoxComponent extends HTMLElement {
constructor() {
super();
this.inputElem = document.createElement("INPUT");
this.inputElem.type = "checkbox";
this.inputElem.checked = false;
this.inputElem.addEventListener('change', () => {
this.checked = this.inputElem.checked;
})
this.attachShadow({
mode: "open"
});
this.shadowRoot.appendChild(this.inputElem);
}
static get observedAttributes() {
return ["checked"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) return;
if (oldValue === null) { // attribute was added
this.inputElem.checked = true;
} else if (newValue === null) { // attribute was removed
this.inputElem.checked = false;
}
const event = new CustomEvent('bit-box-change', {
detail: {
checked: this.checked
}
});
this.dispatchEvent(event);
}
get checked() {
return this.inputElem.checked;
}
set checked(bool) {
if (bool) {
this.setAttribute('checked', '');
} else {
this.removeAttribute('checked')
}
}
}
customElements.define("bit-box", BitBoxComponent);
document.querySelectorAll('bit-box').forEach(box =>
box.addEventListener('bit-box-change', (event) => {
console.log(event.detail.checked);
})
);
<bit-box id="bit-7"></bit-box>
<bit-box id="bit-6"></bit-box>
<bit-box id="bit-0"></bit-box>
<!-- https://github.com/mateusortiz/webcomponents-the-right-way -->
属性如何工作:布尔属性如checked
, disabled
,hidden
工作方式如下:value
属性的实际值是无关紧要的。属性存在表示,true
不存在表示假。
另请注意,在常规复选框上,该属性checked
仅反映复选框的初始状态。当它改变时,这不会反映到属性上!您真正感兴趣的不是checked
属性,而是checked
属性。
推荐阅读
- opencart-3 - 类别页面上的 Opencart 产品类别 ID
- reactjs - 如何在 React Native 的选项卡中嵌套导航?
- python - 如何在 Python 中检查视频是否有声音?
- css - 如何在 Angular 组件中设置 ::before 伪元素的 CSS 属性?
- ios - 在 IBAgent NSInternalInconsistencyException 之后无法在 Xcode 12 中编辑情节提要:无法创建工作间隔
- flutter - 如何从 Flutter 移动应用程序控制 Rapsberry pi?
- excel - 如何提取约会,包括重复?
- python - 操作/复制数据框(熊猫)并仅维护每个月的最后一天
- python - 如何将Kafka的时间戳转换为日期时间?
- java - 等效于Java中的时刻反应