向事件处理程序函数添加参数
Add Argument to event-handler function?
我正在与Polymer合作一个小型web项目。
我正在为项目列表中的每个项目显示一个删除按钮。删除按钮触发deleteItem()
-功能。我想添加item.id
或item
本身作为参数,这样我就可以删除正确的项。
我该怎么做
<template id="bind" is="dom-bind">
<script>
var bind = document.querySelector('#bind');
bind.deleteItem = function() {
// Get item id?
}
</script>
<template is="dom-repeat" items="{{data}}">
<span>{{item.name}}</span>
<paper-button on-click="deleteItem" id="{{item.id}}">Delete</paper-button></p>
</template>
</template>
您不能向事件处理程序传递额外的参数,但可以获得对event.model
模型的引用。
请参阅https://www.polymer-project.org/1.0/docs/devguide/templates.html#handling-示例的事件
<dom-module id="simple-menu">
<template>
<template is="dom-repeat" id="menu" items="{{menuItems}}">
<div>
<span>{{item.name}}</span>
<span>{{item.ordered}}</span>
<button on-click="order">Order</button>
</div>
</template>
</template>
<script>
Polymer({
is: 'simple-menu',
ready: function() {
this.menuItems = [
{ name: "Pizza", ordered: 0 },
{ name: "Pasta", ordered: 0 },
{ name: "Toast", ordered: 0 }
];
},
order: function(e) {
var model = e.model; // <== get the model from the clicked item
model.set('item.ordered', model.item.ordered+1);
}
});
</script>
</dom-module>
相关文章:
- 如何向这个javascript/jquery函数添加参数
- 如何在所有ajax调用中向URL添加参数
- 在Javascript中向回调添加参数
- javascript窗口打开并添加参数
- 无法在 JavaScript 函数中添加参数
- 向jQueryajax调用添加参数,最好添加到URL或对象中
- 使用JavaScript向链接添加参数
- 正在向传递的回调添加参数
- 循环遍历数组,并在javascript中向sql查询添加参数
- 向回调添加参数
- 通过随机 url 重定向传递和添加参数
- 如何使用变量向对象添加参数
- 为什么添加参数时停止功能
- 向网址添加参数 - 因现有参数而异
- 当函数绑定在构造函数中时,如何在 ES6 React 中向事件处理程序添加参数
- 使用 JavaScript 或 jQuery 如何在页面加载后向 URL 添加参数
- 添加参数时调用javascript函数停止工作,这真的不大声吗
- 余烬数据不会向后置请求添加参数
- 如何在函数调用中自动添加参数
- 为什么可以在 JavaScript 中添加参数类型和返回类型