[英]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.