Html 如何从href调用javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16337937/
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-08-29 08:04:56  来源:igfitidea点击:

How to call javascript from a href?

javascripthtml

提问by user2033281

How to call javascript from a href?

如何从href调用javascript?

like:

喜欢:

<a href="<script type='text/javascript'>script code</script>/">Call JavaScript</a>

回答by Dawson

<a onClick="yourFunction(); return false;" href="fallback.html">One Way</a>

<a onClick="yourFunction(); return false;" href="fallback.html">One Way</a>

** Edit**
From the flurry of comments, I'm sharing the resources given/found.

**编辑**
从一连串的评论中,我正在分享给定/找到的资源。

Previous SO Q and A's:

以前的 SO Q 和 A 的:

Interesting reads:

有趣的朗读:

回答by bwoebi

<a href="javascript:call_func();">...</a>

where the function then has to return false so that the browser doesn't go to another page.

然后该函数必须返回 false 以便浏览器不会转到另一个页面。

But I'd recommend to use jQuery (with $(...).click(function () {})))

但我建议使用 jQuery (with $(...).click(function () {})))

回答by NilsH

The proper way to invoke javascript code when clicking a link would be to add an onclickhandler:

单击链接时调用 javascript 代码的正确方法是添加onclick处理程序:

<a href="#" onclick="myFunction()">LinkText</a>

Although an even "more proper" way would be to get it out of the html all together and add the handler with another javascript when the dom is loaded.

尽管更“更合适”的方法是将它从 html 中全部取出,并在加载 dom 时使用另一个 javascript 添加处理程序。

回答by NilsH

I would avoid inline javascript altogether, and as I mentioned in my comment, I'd also probably use <input type="button" />for this. That being said...

我会完全避免内联 javascript,正如我在评论中提到的,我也可能会使用<input type="button" />它。话虽如此...

<a href="http://stackoverflow.com/questions/16337937/how-to-call-javascript-from-a-href" id="mylink">Link.</a>


var clickHandler = function() {
     alert('Stuff happens now.');   
}


if (document.addEventListener) {
    document.getElementById('mylink').addEventListener('click', clickHandler, false);
} else {
    document.getElementById('mylink').attachEvent('click', clickHandler);
}


http://jsfiddle.net/pDp4T/1/

http://jsfiddle.net/pDp4T/1/

回答by Ahmet DAL

Using JQuery would be good;

使用 JQuery 会很好;

<a href="#" id="youLink">Call JavaScript </a>



$("#yourLink").click(function(e){
//do what ever you want...
});

回答by KernelPanik

JavaScript code is usually called from the onclickevent of a link. For example, you could instead do:

JavaScript 代码通常从onclick链接事件中调用。例如,您可以改为:

In Head Section of HTML Document

在 HTML 文档的头部

<script type='text/javascript'>
function myFunction(){
//...script code
}
</script>

In Body of HTML Document

在 HTML 文档正文中

<a href="#" id="mylink" onclick="myFunction(); return false">Call JavaScript </a>

Alternatively, you can also attach your function to the link using the links' ID, and HTML DOM or a framework like JQuery.

或者,您也可以使用链接的 ID、HTML DOM 或 JQuery 等框架将您的函数附加到链接。

For example:

例如:

In Head Section of HTML Document

在 HTML 文档的头部

<script type='text/javascript'>
document.getElementById("mylink").onclick = function myFunction(){ ...script code};
</script>

In Body of HTML Document

在 HTML 文档正文中

<a href="#" id="mylink">Call JavaScript </a>

回答by Sean Ciminello

Not sure why this worked for me while nothing else did but just in case anyone else is still looking...

不知道为什么这对我有用而没有其他任何作用,但以防万一其他人仍在寻找......

In between head tags:

在 head 标签之间:

<script>
     function myFunction() {
           script code
     }
</script>

Then for the < a > tag:

然后对于 < a > 标签:

<a href='' onclick='myFunction()' > Call Function </a>

回答by vanessen

another way is :

另一种方法是:

<a href="javascript:void(0);" onclick="alert('testing')">

回答by Yannick G

If you only want to process a function and not process the href it self, add the return false statement at the end of your function:

如果您只想处理一个函数而不是自己处理 href,请在您的函数末尾添加 return false 语句:

 <a href="#" onclick="javascript: function() {... ; return false} return false">click</>

回答by Rishi

Edit This will create a link with Edit after clicking on editing a function name as editwill be called.

编辑 这将在单击编辑函数名称后创建一个与编辑的链接,因为将调用编辑