【发布时间】:2011-12-31 12:17:15
【问题描述】:
问题来了: 我在页面上有一个项目列表,每个项目都有自己的链接/按钮来删除它。目前,我在每个按钮上都有一个 onclick,因为每个按钮都需要将项目的 id 和描述/名称传递给 JS 函数。
我想让它不那么突兀,但想不出办法。有什么建议吗?
这是当前设置的示例:
function doButtonThings(id, desc) {
//jQuery to do some stuff and then remove the item from the page
}
还有页面:
<!-- standard html page stuff -->
<ul>
<li><button onclick="doButtonThings(1001, 'The thing we delete');"></button> Some thing that can be deleted #1</li>
<!-- imagine many more list items this way with different ids and descriptions -->
</ul>
<!-- standard end of html page stuff -->
【问题讨论】:
-
您可以添加一个jQuery事件处理程序,当它被点击时遍历DOM并获取您想要的ID(甚至是元素)。
-
我喜欢这个解决方案,因为它非常简单。我想我可以使用 name 属性作为描述,使用 id 作为 ID,是的。
标签: jquery click inline handler unobtrusive