![](/img/trans.png)
[英]How can I add custom attributes to all heading elements in WordPress?
[英]How to add attributes to custom elements?
我試圖在自定義元素中添加屬性,並在新元素中使用它,但是語法很難。 我看過這篇文章 ,但不清楚它的用法。 如何在創建的回調中聲明和使用自定義“標簽”屬性來呈現它?
<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>
也許我弄錯了,但是您是否嘗試過使用setAttribute()
?
我也不能嘗試registerElement
,但是它以“通常”的方式工作:
var el = document.createElement( 'current-date' );
el.setAttribute( 'label', new Date() );
document.body.appendChild( el );
您的情況應該是這樣的:
createdCallback: function() {
this.innerHTML = this.foo + new Date();
this.setAttribute( 'label', this.getAttribute( 'label' ) + new Date() );
}
您可能正在尋找的是attributeChangedCallback
,這將使您有機會在設置,刪除或修改屬性時運行用戶代碼-> http://w3c.github.io/webcomponents/spec/custom/ #類型-的-回調
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.