How to call JavaScript function instead of href in HTML

前端 未结 7 1278
执念已碎
执念已碎 2020-12-12 20:18

I have some mockup in HTML

\"next

        
相关标签:
7条回答
  • 2020-12-12 20:32

    That syntax should work OK, but you can try this alternative.

    <a href="javascript:void(0);" onclick="ShowOld(2367,146986,2);">
    

    or

    <a href="javascript:ShowOld(2367, 146986, 2);">
    

    UPDATED ANSWER FOR STRING VALUES

    If you are passing strings, use single quotes for your function's parameters

    <a href="javascript:ShowOld('foo', 146986, 'bar');">
    
    0 讨论(0)
  • 2020-12-12 20:35

    Your should also separate the javascript from the HTML.
    HTML:

    <a href="#" id="function-click"><img title="next page" alt="next page" src="/themes/me/img/arrn.png"></a>
    

    javascript:

    myLink = document.getElementById('function-click');
    myLink.onclick = ShowOld(2367,146986,2);
    

    Just make sure the last line in the ShowOld function is:

    return false;
    

    as this will stop the link from opening in the browser.

    0 讨论(0)
  • 2020-12-12 20:36

    href is optional for a elements.

    It's completely sufficient to use

    <a onclick="ShowOld(2367,146986,2)">link text</a>
    
    0 讨论(0)
  • 2020-12-12 20:38

    I use a little CSS on a span to make it look like a link like so:

    CSS:

    .link {
        color:blue;
        text-decoration:underline;
        cursor:pointer;
    }
    

    HTML:

    <span class="link" onclick="javascript:showWindow('url');">Click Me</span>
    

    JAVASCRIPT:

    function showWindow(url) {
        window.open(url, "_blank", "directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
    }
    
    0 讨论(0)
  • 2020-12-12 20:44

    If you only have as "click event handler", use a <button> instead. A link has a specific semantic meaning.

    E.g.:

    <button onclick="ShowOld(2367,146986,2)">
        <img title="next page" alt="next page" src="/themes/me/img/arrn.png">
    </button>
    
    0 讨论(0)
  • 2020-12-12 20:50

    <a href="#" onclick="javascript:ShowOld(2367,146986,2)">

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