开发者

inline javascript in href

How can you do something like this:

<a href="some javascript statement that isn't a function call;" >开发者_JS百科;myLink</a>

And have the js in the href execute when the link is clicked.


Just put the JS code directly in there:

<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>


<a href="javascript:var hi = 3;" >myLink</a>

Now you can use hi anywhere to get 3.


You can write inline-code in the same way as <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

<a onClick="function()">


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.

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&amp;body=Hi");
});
<a id="loginMailLink" href= "#" style="color:blue">CACS GNS Site Admin.</a>

I hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜