【发布时间】:2011-05-24 14:03:35
【问题描述】:
我想以编程方式将一系列<li> 元素添加到<ul>,并为每个元素添加一个点击事件。
我不知道该怎么做,至少不是以简洁的 jQueryish 方式。
这是我现有的代码:
<ul id="saved-list"></ul>
<script type="text/javascript">
$.each(all_objects, function() {{
var list_route = "<li><a href='#saved-route'>" + this.text + "</a></li>";
$('#saved-list').append(list_route);
// add unique id (this.id) to item and click event here?
// pseudocode - onclick: alert(this.id);
});
$('#saved-list').refresh('listview'); // jquery mobile list refresh
</script>
请有人建议如何以编程方式向每个列表项添加点击事件?
更新:我需要为每个列表项做一些稍微不同的事情(我们只是说一个警报) - 很抱歉没有说清楚。
【问题讨论】:
-
您的 ul id 属性缺少引号,并且脚本周围没有脚本块。复制/粘贴错误?
-
@jvenema,谢谢,已编辑!
标签: javascript jquery jquery-mobile