href 中的内联 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5519059/
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
inline javascript in href
提问by PhilBrown
How can you do something like this:
你怎么能做这样的事情:
<a href="some javascript statement that isn't a function call;" >myLink</a>
And have the js in the href execute when the link is clicked.
并在单击链接时执行 href 中的 js。
采纳答案by pimvdb
<a href="javascript:var hi = 3;" >myLink</a>
Now you can use hi
anywhere to get 3
.
现在您可以hi
在任何地方使用来获取3
.
回答by meder omuraliev
Just put the JS code directly in there:
直接把JS代码放在里面就行了:
<a href="#" onclick="a=1;b=2; return false;">fsljk</a>
Though, you should not be doing inline scripting. You should unobtrusively attach event handlers.
但是,您不应该进行内联脚本编写。您应该不显眼地附加事件处理程序。
<a id="lol" href="/blah">fdsj</a>
<script>
document.getElementById('lol').onclick=function() {
/* code */
};
</script>
回答by ??-mung
You can write inline-code in the same way as
<a href="javascript:[code]">
您可以使用与以下相同的方式编写内联代码
<a href="javascript:[code]">
This method is also available in other tags.
此方法在其他标签中也可用。
addition
添加
if you want to Execution when something tag clicking, you can fix with onClick attribute
如果你想在点击某个标签时执行,你可以用 onClick 属性修复
<a onClick="function()">
<a onClick="function()">
回答by Madog
I know this question is old but I had a similar challenge dynamically modifying the href attribute that sends an email when the anchor tag is clicked.
我知道这个问题很老,但我遇到了类似的挑战,动态修改在单击锚标记时发送电子邮件的 href 属性。
The solution I came up with is this:
我想出的解决方案是这样的:
$('#mailLink,#loginMailLink,#sbMailLink').click(function () {
this.setAttribute('href', "mailto:" + sessionStorage.administrator_mail_address + "?subject=CACS GNS Portal - Comments or Request For Access&body=Hi");
});
<a id="loginMailLink" href= "#" style="color:blue">CACS GNS Site Admin.</a>
I hope that helps.
我希望这有帮助。