单击超链接时调用 javascript 函数 [英] call javascript function on hyperlink click
本文介绍了单击超链接时调用 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 ASP.NET 的 c# 代码隐藏文件中动态创建超链接.我需要在客户端单击时调用 JavaScript 函数.我该如何实现?
I am dynamically creating a hyperlink in the c# code behind file of ASP.NET. I need to call a JavaScript function on client click. how do i accomplish this?
推荐答案
更整洁,而不是典型的 href="#"
或 href="javascript:void"
或 href="whatever"
,我认为这更有意义:
Neater still, instead of the typical href="#"
or href="javascript:void"
or href="whatever"
, I think this makes much more sense:
var el = document.getElementById('foo');
el.onclick = showFoo;
function showFoo() {
alert('I am foo!');
return false;
}
<a href="no-javascript.html" title="Get some foo!" id="foo">Show me some foo</a>
如果 Javascript 失败,会有一些反馈.此外,还消除了不稳定行为(href="#"
情况下的页面跳转,href=""
情况下访问同一页面).
If Javascript fails, there is some feedback. Furthermore, erratic behavior (page jumping in the case of href="#"
, visiting the same page in the case of href=""
) is eliminated.
这篇关于单击超链接时调用 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文