gpt4 book ai didi

javascript - 如何给自定义元素添加属性?

转载 作者:行者123 更新时间:2023-11-28 01:48:04 27 4
gpt4 key购买 nike

我正在尝试在我的自定义元素中添加属性并在我的新元素中使用它,但在语法方面遇到困难。我看过this article ,但不是很清楚它的用法。如何在我创建的回调中声明和使用自定义“标签”属性来呈现它?

<current-date foo="Today is: "></current-date>

<script>
document.registerElement('current-date', {
prototype: {
createdCallback: function() {
this.innerHTML = this.foo + new Date();
},
foo: {
value: function() {
alert('foo() called');
}
}
}
});
</script>

http://jsbin.com/UdOwizU/4/ (仅适用于 Google Canary)

最佳答案

也许是这样的:

<body>
<current-date label="Today is: "></current-date>

<script>
document.registerElement('current-date', {
prototype: {
createdCallback: function() {
this.foo = {value: function() {
return this.attributes.getNamedItem("label").value;
}},
this.innerHTML = this.foo.value.call(this) + new Date();
}
}
});
</script>
</body>

http://jsbin.com/ivaWUyAL/3/edit

关于javascript - 如何给自定义元素添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393521/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com