我刚刚创建了我的第一个元素作为测试,我遇到了点击事件的问题。我的元素只是一个带有单击事件的按钮,用于增加一个计数器,该计数器显示为按钮文本的一部分。我还添加了一个内容标签,以便您可以向按钮添加其他文本。
当我点击 Chrome 中的按钮时,如果我点击内容文本,则不会触发 click 事件。我必须实际单击按钮元素/按钮中的计数才能触发事件。尽管在 Firefox、Safari 和 Opera 中,一切似乎都正常工作。我可以在这些浏览器中单击按钮上的任何位置,然后单击事件将触发。难道我做错了什么?这只是 Chrome 的一个错误吗?这是我的代码:
索引.html
<!DOCTYPE html>
<html>
<head>
<title>Test Element</title>
<script src="bower_components/platform/platform.js"></script>
<link rel="import" href="elements/my-counter.html">
</head>
<body unresolved touch-action="auto">
<my-counter count=5>Times a Day</my-counter>
</body>
</html>
元素/my-counter.html
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="my-counter" attributes="count">
<template>
<button on-click="{{increment}}">{{ count }} <content></content></button>
</template>
<script>
Polymer('my-counter', {
count: 0,
increment: function(event, detail, sender) {
console.log(event, detail, sender);
++this.count;
}
});
</script>
</polymer-element>
如果您想下载并自行测试,此代码也在 GitHub 上:https ://github.com/andersryanc/playing-with-polymer/tree/master/my-counter