【问题标题】:how to write onclick event for href tag如何为 href 标签编写 onclick 事件
【发布时间】:2012-05-31 15:27:09
【问题描述】:
谁能帮我在href标签上的onClick事件上写这个?
string x= "<a href=\"JavaScript:callfunction(event,'"+ y.id.tostring() + "');\">click</a>";
【问题讨论】:
标签:
javascript
jquery
asp.net
asp.net-mvc
asp.net-mvc-2
【解决方案1】:
string x = string.Format(
"<a href=\"#\" onclick=\"callfunction(event, '{0}')\">click</a>",
y.id
);
【解决方案2】:
为什么不这样:
string x= "<a href=\"#\" onclick=\"callfunction(event,'"+ y.id.tostring() + "');\">click</a>";
【解决方案3】:
只是一种使用 DOM 负责方法来完成此类任务的替代方法
var x = document.createElement('a');
x.href = "#";
x.onclick = function(evt) {
callfunction(evt, y.id.tostring())
}
<destination-node>.appendChild(x);