JavaScript function in href vs. onclick

后端 未结 15 2010
栀梦
栀梦 2020-11-22 07:17

I want to run a simple JavaScript function on a click without any redirection.

Is there any difference or benefit between putting the JavaScript call in the hr

相关标签:
15条回答
  • 2020-11-22 07:43

    In terms of javascript, one difference is that the this keyword in the onclick handler will refer to the DOM element whose onclick attribute it is (in this case the <a> element), whereas this in the href attribute will refer to the window object.

    In terms of presentation, if an href attribute is absent from a link (i.e. <a onclick="[...]">) then, by default, browsers will display the text cursor (and not the often-desired pointer cursor) since it is treating the <a> as an anchor, and not a link.

    In terms of behavior, when specifying an action by navigation via href, the browser will typically support opening that href in a separate window using either a shortcut or context menu. This is not possible when specifying an action only via onclick.


    However, if you're asking what is the best way to get dynamic action from the click of a DOM object, then attaching an event using javascript separate from the content of the document is the best way to go. You could do this in a number of ways. A common way is to use a javascript library like jQuery to bind an event:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <a id="link" href="http://example.com/action">link text</a>
    <script type="text/javascript">
        $('a#link').click(function(){ /* ... action ... */ })
    </script>
    
    0 讨论(0)
  • 2020-11-22 07:45

    it worked for me using this line of code:

    <a id="LinkTest" title="Any Title"  href="#" onclick="Function(); return false; ">text</a>
    
    0 讨论(0)
  • 2020-11-22 07:45

    I experienced that the javascript: hrefs did not work when the page was embedded in Outlook's webpage feature where a mail folder is set to instead show an url

    0 讨论(0)
  • 2020-11-22 07:46

    First, having the url in href is best because it allows users to copy links, open in another tab, etc.

    In some cases (e.g. sites with frequent HTML changes) it is not practical to bind links every time there is an update.

    Typical Bind Method

    Normal link:

    <a href="https://www.google.com/">Google<a/>
    

    And something like this for JS:

    $("a").click(function (e) {
        e.preventDefault();
        var href = $(this).attr("href");
        window.open(href);
        return false;
    });
    

    The benefits of this method are clean separation of markup and behavior and doesn't have to repeat the function calls in every link.

    No Bind Method

    If you don't want to bind every time, however, you can use onclick and pass in the element and event, e.g.:

    <a href="https://www.google.com/" onclick="return Handler(this, event);">Google</a>
    

    And this for JS:

    function Handler(self, e) {
        e.preventDefault();
        var href = $(self).attr("href");
        window.open(href);
        return false;
    }
    

    The benefit to this method is that you can load in new links (e.g. via AJAX) whenever you want without having to worry about binding every time.

    0 讨论(0)
  • 2020-11-22 07:48

    I use

    Click <a nohref style="cursor:pointer;color:blue;text-decoration:underline"
    onClick="alert('Hello World')">HERE</a>
    

    A long way around but it gets the job done. use an A style to simplify then it becomes:

    <style> A {cursor:pointer;color:blue;text-decoration:underline; } </style> 
    <a nohref onClick="alert('Hello World')">HERE</a>
    
    0 讨论(0)
  • 2020-11-22 07:48

    In addition to all here, the href is shown on browser's status bar, and onclick not. I think it's not user friendly to show javascript code there.

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