href与onclick中的JavaScript函数

JavaScript function in href vs. onclick

我想在单击时运行一个简单的javascript函数,而不进行任何重定向。

把javascript调用放在href属性中是否有任何区别或好处(如:

1
....

)与将其放入onclick属性(绑定到onclick事件)相比?


(P)坏:(p)字母名称(P)Good:(p)字母名称(P)Better:(p)字母名称(P)Even Better 1:(p)字母名称(P)Even Better 2:(p)字母名称(P)为什么贝蒂?Because EDOCX1 penography 17 lonible will prevent browser from following the link(p)(P)最佳:(p)(P)利用JQERY或其他类似的框架,通过Element's Id打击Onclik Handler。(p)字母名称


(P)在人权与平等机会委员会内执行Onclik将意味着那些相信从行为/行动中分离内容的人。The argument is that your html content should remain focused solely on content,not on presentation or behavior.(p)(P)The typical path these days is to use a Javascript Library(EG.Jquery)and create an event handler using that library.它会看起来像一些东西:(p)字母名称


(P)In terms of Javascript,one difference is that the EDOCX1 original 2-keyword in the EDOCX1 universifical 1-handler will refer to the dom element whose EDOCX1 indicategory 1-attribute it is(in this case the EDOCX1 reuniversal 5 element),whereas EDOCX1 silginal 2 in the EDOCX1 in the EDOCX1 7 attribute will refer the EDOCX1.(p)(P)In terms of presentation,if an EDOCX1 penogical 7 attribute is absent from a link(I.E.EDOCX1 pental 5)then,by default,browsers will display the EDOCX1 penogical 11 silver(and not the often-desired EDOCX1)since it is treating the EDOCX1 as an anchor,and not a link.(p)(P)In terms of behavior,when specifying an action by navigation via EDOCX1 penal 7,the browser will typically support opening that EDOCX1 penal 7 in a separate window using either a shortcut or context menu.这是不可能的,当具体规定只有通过EDOCX1的方式采取行动时。(p)(P)However,if you're asking what is the best way to get dynamic action from the click of a dom object,they 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:(p)字母名称


(P)我使用(p)字母名称(P)长途跋涉,但它得到了工作的礼物。用一种方式来简单化Then it becomes:(p)字母名称


(P)The best way to do this is with:(p)字母名称(P)The problem is that this will add a hash($35);to the end of the page's URL in the browser,thus requiring the user to clik the back but ton twice to go to the page before your rs.考虑到这一点,你需要增加一些守则来阻止事件的传播。大多数Javascript Toolkits will already have a function for this.For example,the dojo toolkit used(p)字母名称(P)对,对(p)


(P)In addition to all here,the href is shown on browser's status bar,and onclik not.我想这不是用朋友来展示Javascript code there.(p)


(P)我用这条代码线工作:(p)字母名称


(P)Having EDOCX1 o niki in any attribute that isn't specifically for scripting is an outdated method of html.While technically it works,you're still assigning javascript properties to a non-script attribute,which isn't good practice.它甚至可以对老布劳斯人或甚至一些现代人产生影响(一个Googled Forum Post Seemd to Indicate that Opera does not like'Javascript:'URLS)。(p)(P)a better practice would be the second way,to put your javascript into the EDOCX1 universal 1-attribute,which is ignored if no scripting functionality is available.Place a valid url in the HREF field(Commonly''s 35;")for fallback for those who do not have Javascript.(p)


这作品

1
Click Here


最重要的答案是一个非常糟糕的做法,你永远不应该链接到一个空的哈希表,因为它会在道路上造成问题。

最好的方法是像其他许多人所说的那样将事件处理程序绑定到元素上,但是,do stuff在每个现代浏览器中都能完美地工作,并且在呈现模板时广泛使用它,以避免为每个实例重新绑定。在某些情况下,这种方法提供了更好的性能。牛传染性胃肠炎病毒

另一个有趣的tid位…

直接调用javascript时,onclickhref有不同的行为。

onclick将正确地通过this上下文,而href将不会,或者换句话说,no context将不起作用,而no context将起作用。

是的,我省略了href。虽然这不符合规范,但它可以在所有浏览器中使用,不过理想情况下,它应该包含一个href="javascript:void(0);",以便更好地度量。


(P)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.然而,这是一个唯一的方法。(p)


在对javascript使用"href"时,我还注意到一件事:

如果两次单击之间的时间差非常短,则不会执行"href"属性中的脚本。

例如,尝试运行以下示例并双击(快速!)在每个环节上。第一个链接只执行一次。第二个链接将执行两次。

1
2
3
4
5
6
7
8
9
10
function myFunc() {
    var s = 0;
    for (var i=0; i<100000; i++) {
        s+=i;
    }
    console.log(s);
}

href
onclick

在Chrome(双击)和IE11(三次单击)中复制。在Chrome中,如果单击速度足够快,则可以单击10次,只执行1个函数。

火狐工作正常。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
            <h3 class="form-signin-heading"><i class="icon-edit"> Register
            <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>
           
                <button data-placement="top" id="signin_teacher" onclick="window.location='guru/signup_teacher.php'" name="login" class="btn btn-info" type="submit">Teacher</button>
           
       
            <script type="text/javascript">
                $(document).ready(function(){
                $('#signin_student').tooltip('show'); $('#signin_student').tooltip('hide');
                });
               
            <script type="text/javascript">
                $(document).ready(function(){
                $('#signin_teacher').tooltip('show'); $('#signin_teacher').tooltip('hide');
                });

首先,在href中使用URL是最好的,因为它允许用户复制链接,在另一个选项卡中打开等等。

在某些情况下(例如经常更改HTML的站点),每次更新都绑定链接是不实际的。

典型绑定方法

正常链路:

1
Google

对于JS来说是这样的:

1
2
3
4
5
6
$("a").click(function (e) {
    e.preventDefault();
    var href = $(this).attr("href");
    window.open(href);
    return false;
});

这种方法的好处是标记和行为的清晰分离,并且不必在每个链接中重复函数调用。

没有Bind Method

但是,如果不希望每次都绑定,可以在元素和事件中使用onclick和pass,例如:

1
Google

对于JS:

1
2
3
4
5
6
function Handler(self, e) {
    e.preventDefault();
    var href = $(self).attr("href");
    window.open(href);
    return false;
}

这种方法的好处是,您可以随时加载新的链接(例如,通过Ajax),而不必每次都担心绑定。


我体验到,当页面嵌入到Outlook的网页功能中时,javascript:hrefs不起作用,该功能将邮件文件夹设置为显示URL。