JavaScript function in href vs. onclick

后端 未结 15 2011
栀梦
栀梦 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:48
     <hr>
                <h3 class="form-signin-heading"><i class="icon-edit"></i> Register</h3>
                <button data-placement="top" id="signin_student" onclick="window.location='signup_student.php'" id="btn_student" name="login" class="btn btn-info" type="submit">Student</button>
                <div class="pull-right">
                    <button data-placement="top" id="signin_teacher" onclick="window.location='guru/signup_teacher.php'" name="login" class="btn btn-info" type="submit">Teacher</button>
                </div>
            </div>
                <script type="text/javascript">
                    $(document).ready(function(){
                    $('#signin_student').tooltip('show'); $('#signin_student').tooltip('hide');
                    });
                </script>   
                <script type="text/javascript">
                    $(document).ready(function(){
                    $('#signin_teacher').tooltip('show'); $('#signin_teacher').tooltip('hide');
                    });
                </script>   
    
    0 讨论(0)
  • 2020-11-22 07:52

    bad:

    <a id="myLink" href="javascript:MyFunction();">link text</a>
    

    good:

    <a id="myLink" href="#" onclick="MyFunction();">link text</a>
    

    better:

    <a id="myLink" href="#" onclick="MyFunction();return false;">link text</a>
    

    even better 1:

    <a id="myLink" title="Click to do something"
     href="#" onclick="MyFunction();return false;">link text</a>
    

    even better 2:

    <a id="myLink" title="Click to do something"
     href="PleaseEnableJavascript.html" onclick="MyFunction();return false;">link text</a>
    

    Why better? because return false will prevent browser from following the link

    best:

    Use jQuery or other similar framework to attach onclick handler by element's ID.

    $('#myLink').click(function(){ MyFunction(); return false; });
    
    0 讨论(0)
  • 2020-11-22 07:57

    Putting the onclick within the href would offend those who believe strongly in separation of content from behavior/action. The argument is that your html content should remain focused solely on content, not on presentation or behavior.

    The typical path these days is to use a javascript library (eg. jquery) and create an event handler using that library. It would look something like:

    $('a').click( function(e) {e.preventDefault(); /*your_code_here;*/ return false; } );
    
    0 讨论(0)
  • 2020-11-22 08:01

    The top answer is a very bad practice, one should never ever link to an empty hash as it can create problems down the road.

    Best is to bind an event handler to the element as numerous other people have stated, however, <a href="javascript:doStuff();">do stuff</a> works perfectly in every modern browser, and I use it extensively when rendering templates to avoid having to rebind for each instance. In some cases, this approach offers better performance. YMMV

    Another interesting tid-bit....

    onclick & href have different behaviors when calling javascript directly.

    onclick will pass this context correctly, whereas href won't, or in other words <a href="javascript:doStuff(this)">no context</a> won't work, whereas <a onclick="javascript:doStuff(this)">no context</a> will.

    Yes, I omitted the href. While that doesn't follow the spec, it will work in all browsers, although, ideally it should include a href="javascript:void(0);" for good measure

    0 讨论(0)
  • 2020-11-22 08:02

    This works

    <a href="#" id="sampleApp" onclick="myFunction(); return false;">Click Here</a>
    
    0 讨论(0)
  • 2020-11-22 08:03

    Personally, I find putting javascript calls in the HREF tag annoying. I usually don't really pay attention to whether or not something is a javascript link or not, and often times want to open things in a new window. When I try doing this with one of these types of links, I get a blank page with nothing on it and javascript in my location bar. However, this is sidestepped a bit by using an onlick.

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