我正在尝试创建一个Web组件。该组件刚刚取得了进展。 以下是我的代码。
class MyElement extends HTMLElement {
static get observedAttributes() {
return ['value'];
}
get value() {
return this.hasAttribute('value');
}
set value(val) {
if(val) {
this.setAttribute('value', val);
}
else {
this.removeAttribute('value')
}
setValue(val);
}
constructor() {
super();
let shadowRoot = this.attachShadow({mode: 'open'});
const t = document.querySelector('#my-element');
const instance = t.content.cloneNode(true);
shadowRoot.appendChild(instance);
this.shadowDOM = shadowRoot;
}
attributeChangedCallback(name, oldValue, newValue) {
if (this.value) {
this.setValue(newValue);
}
}
// Set a value to progress
setValue(val) {
// How to do?
}
}
customElements.define('my-element', MyElement);

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>index</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<my-element value=50></my-element>
</div>
<button onclick="test()" style="bottom: 10px; right: 10px; position: absolute;">test</button>
<script>
function test() {
// How to set the value to my-element?
}
</script>
<!--My Element-->
<template id="my-element">
<style>
:host {
position: relative;
display: block;
width: 600px;
}
progress {
-webkit-appearance: none;
-moz-appearance: none;
}
progress::-moz-progress-bar {
background: gainsboro;
width: 300px;
height: 500px;
}
progress::-moz-progress-value {
background: green;
}
progress::-webkit-progress-bar {
background: gainsboro;
width: 300px;
height: 500px;
}
progress::-webkit-progress-value {
background: green;
}
</style>
<progress value=20 max=100>
</progress>
</template>
</body>
</html>
&#13;
我想通过my-element的值来控制进度值。
我在my-element(<my-element value=50></my-element>)
中设置了一个值,但它不起作用,我也不知道如何通过js设置值。
我猜测问题是setValue(val)
课程中的MyElement
,但我不知道如何实施。
答案 0 :(得分:2)
我想说你只需要使用DOM并检索你的进度元素。
class MyElement extends HTMLElement {
static get observedAttributes() {
return ['value'];
}
get value() {
return this.hasAttribute('value');
}
set value(val) {
if(val) {
this.setAttribute('value', val);
}
else {
this.removeAttribute('value')
}
}
constructor() {
super();
let shadowRoot = this.attachShadow({mode: 'open'});
const t = document.querySelector('#my-element');
const instance = t.content.cloneNode(true);
shadowRoot.appendChild(instance);
this.shadowDOM = shadowRoot;
}
attributeChangedCallback(name, oldValue, newValue) {
if (this.value) {
this.setValue(newValue);
}
}
// Set a value to progress
setValue(val) {
const progress = this.shadowDOM.lastElementChild;
progress.value = val;
}
}
customElements.define('my-element', MyElement);
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>index</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<my-element id="myElement" value=50></my-element>
</div>
<button onclick="test()" style="bottom: 10px; right: 10px; position: absolute;">test</button>
<script>
function test() {
document.getElementById("myElement").value = 5;
}
</script>
<!--My Element-->
<template id="my-element">
<style>
:host {
position: relative;
display: block;
width: 600px;
}
progress {
-webkit-appearance: none;
-moz-appearance: none;
}
progress::-moz-progress-bar {
background: gainsboro;
width: 300px;
height: 500px;
}
progress::-moz-progress-value {
background: green;
}
progress::-webkit-progress-bar {
background: gainsboro;
width: 300px;
height: 500px;
}
progress::-webkit-progress-value {
background: green;
}
</style>
<progress value=20 max=100>
</progress>
</template>
</body>
</html>
&#13;