带有 onclick/JavaScript 属性的锚标记。停止刷新 IE 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6133653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:37:05  来源:igfitidea点击:

Anchor Tag with onclick/JavaScript attribute. Stop refreshing page in I.E

javascripthtmlanchor

提问by John R

I have a HTML/JavaScript project that holds a few dozen anchor tags; each of the anchor tags calls the same JavaScript function, but with a different parameter.

我有一个包含几十个锚标记的 HTML/JavaScript 项目;每个锚标记调用相同的 JavaScript 函数,但使用不同的参数。

Everything looks good in Firefox and Chrome, but in Internet Explorer (IE) the page seems to reload (flicker) every time I click an anchor tag (like the one shown below). How can I make IE stop reloading/flickering? I would prefer not to rewrite the whole script. I have tried onclcick='javascript... and href='javascript...,but both have the same problem (although onclick seems a little better).

在 Firefox 和 Chrome 中,一切看起来都不错,但在 Internet Explorer (IE) 中,每次单击锚标记(如下所示)时,页面似乎都会重新加载(闪烁)。如何让 IE 停止重新加载/闪烁?我不想重写整个脚本。我试过 onclcick='javascript... 和 href='javascript...,但两者都有相同的问题(虽然 onclick 似乎好一点)。

<a onclick='javascript:foo(22)'></a> 

回答by Shurdoof

Try <a onclick='foo(22); return false;'></a>

尝试 <a onclick='foo(22); return false;'></a>

Also, javascript:is pointless in event attributes as it just defines a label.

此外,javascript:事件属性毫无意义,因为它只是定义了一个标签。

回答by Claudiu

Simpler to use jQuery:

使用 jQuery 更简单:

<a href="#" class="action" rel="22"></a>
<script>
    $('.action').click(function(){
        yourfunction($(this).attr('rel');
        return false;
    });
</script>