【发布时间】:2019-08-30 02:23:07
【问题描述】:
我有一个按预期工作的自定义 HTMLElement,但我找不到将参数传递给构造函数的方法;相反,我正在访问传递自定义标记中的值,但随后我必须在 connectedCallback() 中设置值,我对此并不满意。
这是我现在正在做的一个基本版本:
class TrackingPreview extends HTMLElement {
constructor() {
super();
const video = document.createElement('video');
video.setAttribute('controls', '');
video.setAttribute('width', '1920');
video.setAttribute('height', '1080');
shadow.appendChild(video);
this.video = video;
}
connectedCallback() {
const videoId = this.getAttribute('video-id');
this.video.id = videoId;
}
}
我宁愿将 videoId 直接传递给构造函数,就像这样(不起作用):
JS:
class TrackingPreview extends HTMLElement {
constructor(videoId) {
super();
const video = document.createElement('video');
video.setAttribute('id', videoId);
video.setAttribute('controls', '');
video.setAttribute('width', '1920');
video.setAttribute('height', '1080');
shadow.appendChild(video);
}
connectedCallback() {
}
}
HTML 页面上的 JS 脚本:
$(document).ready(function(){
const tracking_preview = document.createElement('tracking-preview','{{video_id}}');
tracking_preview.videoId = '{{video_id}}';
document.body.append(tracking_preview);
});
有没有办法将值传递给自定义构造函数?文档暗示这是可能的,但对如何做到这一点没有太大帮助。
【问题讨论】:
标签: javascript html django custom-element