我是 hyperHTML 的新手,正在试验它。这是我的问题。如何为重复的模板元素添加事件处理程序。这是我的网络组件:
import css from './component.css';
import isJson from '../utils/isJson.js';
const hyper = hyperHTML;
class IdxAdminTab extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
}
attributeChangedCallback(name, oVal, nVal) {
if (name === 'tabs' && isJson(nVal)) {
this.tabs = JSON.parse(nVal);
this.tabs.forEach((tab) => {
tab.number = parseInt(tab.number, 10).toLocaleString();
});
this.updateView();
}
}
clearSelections() {
this.tabs.forEach((tab) => {
tab.selected = false;
});
}
connectedCallback() {
this.updateView();
}
currentlySelected() {
return this.tabs.find((tab) => tab.selected);
}
disconnectedCallback() {
}
tabSelected(evn) {
}
updateView() {
hyper(this.shadowRoot)`
<style>${css}</style>
<div id="tab-container">
${this.tabs.map(tab => `
<div class="${tab.selected ? 'selected' : ''}" id="${tab.id}" onclick="${this.selectedTab}">
<div class="name">${tab.name}</div>
<div class="number">${tab.number}</div>
</div>
`)}
</div>
`;
}
static get observedAttributes() {
return ['tabs'];
}
}
customElements.define('idx-admin-tab', IdxAdminTab);
export { IdxAdminTab };
我想在重复选项卡中添加一个单击处理程序并将 tabSelected 注册为处理程序。我添加的点击处理程序引发了一个未捕获的语法错误:
(function(event){[object HTMLElement]
})