inline javascript in href

后端 未结 4 1390
猫巷女王i
猫巷女王i 2021-02-05 11:03

How can you do something like this:

myLink

And have the js

相关标签:
4条回答
  • 2021-02-05 11:15

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

    I hope that helps.

    0 讨论(0)
  • 2021-02-05 11:34

    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>
    
    0 讨论(0)
  • 2021-02-05 11:35

    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()">

    0 讨论(0)
  • 2021-02-05 11:38
    <a href="javascript:var hi = 3;" >myLink</a>
    

    Now you can use hi anywhere to get 3.

    0 讨论(0)
提交回复
热议问题