例如,我有这样的代码:
$("#total").html("Price $<span>" + value + "</span>");我还想在那里添加一个href按钮。
$("#total").html("Price $<span>" + value + "</span><a id='remove' href='#'>remove</>);但是我如何为这个href添加onclick事件呢?
更新
我真的很喜欢这个解决方案:
//Create the link to remove the item beforehand, and add the onclick event handler.
var removeLink = $("<a id='remove' href='#'>remove</a>").click(function(e) {
//Click event handler here.
});
$("#total").html("Price $<span>" + value + "</span>"); //Create the price value
$("#total").append(removeLink); //Add the remove link.因为我的表单上将有很多删除链接,但我有一个问题,在这种情况下,如何将任何参数传递给单击函数?
发布于 2011-07-28 20:31:42
你可以用几种方法来做,live()就是一个例子,或者..
$("#total").html("Price $<span>" + value + "</span><a id='remove' href='#'>remove</a>");
$("#remove").click(function() { /* ... */ });或
$("#total").html("Price $<span>" + value + "</span><a id='remove' onclick='doSomething();' href='#'>remove</a>");以及使用集合分配或链接等的其他方式。
https://stackoverflow.com/questions/6865267
复制相似问题