使用本机 Web 组件将日期选择器作为自定义日历
我想使用本机Web来拥有自定义日历。我创建了JS文件,并编写了一些扩展HTMlelement的类。日历正确弹出,但问题是我无法在日历中选择一天。当我尝试选择一个日期时,它会引发此异常:“此datepicker的丢失实例数据”。
我的代码看起来像这样:
class CustomCalendar extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({mode: 'open'});
const wrapper = document.createElement('div');
wrapper.setAttribute('class', 'wrapper');
const calendarInputLabel = wrapper.appendChild(document.createElement('label'));
calendarInputLabel.setAttribute('class', 'date-label');
const iconImg = calendarInputLabel.appendChild(document.createElement('img'));
iconImg.setAttribute('class', 'calendarIcon');
iconImg.src = this.hasAttribute('iconImgSrc') ? this.getAttribute('iconImgSrc') : 'api/theme/img/icons-calendar.png';
const calendarInput = calendarInputLabel.appendChild(document.createElement('input'));
calendarInput.setAttribute('type', 'text');
calendarInput.setAttribute('id', this.getAttribute('calendarName'));
shadowRoot.append(wrapper);
}
connectedCallback() {
$(this.shadowRoot.getElementById('from')).datepicker({
dateFormat: "dd-mm-yy"
, duration: "fast"
});
}
}
customElements.define('custom-calendar', CustomCalendar);
<custom-calendar calendarName="from"> </custom-calendar>
请帮我。我该如何解决?
I wanted to use native web in order to have custom calendar. I created js file and wrote some class that extends HTMLElement. Calendar pops up properly but the problem is that I can't choose a day in the calendar. When I try to choose a date It throws this exception : "Uncaught Missing instance data for this datepicker".
My code looks like this :
class CustomCalendar extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({mode: 'open'});
const wrapper = document.createElement('div');
wrapper.setAttribute('class', 'wrapper');
const calendarInputLabel = wrapper.appendChild(document.createElement('label'));
calendarInputLabel.setAttribute('class', 'date-label');
const iconImg = calendarInputLabel.appendChild(document.createElement('img'));
iconImg.setAttribute('class', 'calendarIcon');
iconImg.src = this.hasAttribute('iconImgSrc') ? this.getAttribute('iconImgSrc') : 'api/theme/img/icons-calendar.png';
const calendarInput = calendarInputLabel.appendChild(document.createElement('input'));
calendarInput.setAttribute('type', 'text');
calendarInput.setAttribute('id', this.getAttribute('calendarName'));
shadowRoot.append(wrapper);
}
connectedCallback() {
$(this.shadowRoot.getElementById('from')).datepicker({
dateFormat: "dd-mm-yy"
, duration: "fast"
});
}
}
customElements.define('custom-calendar', CustomCalendar);
<custom-calendar calendarName="from"> </custom-calendar>
please help me. How can I fix this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论