简体   繁体   English

HTML / jQuery:如何正确调用锚标记上的函数(跨浏览器)

[英]HTML / jQuery: How to properly call a function on an anchor tag (cross-browser)

I have a page with a few links (anchor tags) that are used to call a function. 我有一个页面,其中包含一些用于调用函数的链接(锚标签)。

Everything is working as intended but I wonder what is the proper way here to avoid issues with this cross-browser as I've seen different approaches on Google. 一切都按预期进行,但是我不知道在这里如何避免这种跨浏览器出现问题的正确方法,因为我在Google上看到了不同的方法。

I use jQuery to call the function instead of inline javascript as this way all my functions are called the same way + I want to call the same function from different elements by using a class. 我使用jQuery而不是内联javascript来调用函数,因为这样,我所有的函数都以相同的方式调用+我想通过使用类从不同元素中调用相同的函数。

Can someone tell me if the following is the correct approach here to avoid issues cross-browser (IE8, IE9, FF, Chrome) ? 有人可以告诉我下面是否是避免跨浏览器问题的正确方法(IE8,IE9,FF,Chrome)?

Also, do I have to add one of the following or something similar here to my function ? 另外,我是否必须在函数中添加以下内容之一或类似内容?

return false;

OR 要么

e.preventDefault();

My HTML: 我的HTML:

<a href="javascript:void(0)" class="deco-min showSiteMap">Site Map</a>

My jQuery: 我的jQuery:

$('.showSiteMap').on('click', function() {
    showSiteMap();
});

Since you are using jQuery, the cross browser concers are handled by the library itself so you don't have to worry about it. 由于您使用的是jQuery,因此跨浏览器的作用域由库本身处理,因此您不必担心。 So either of the approaches will work fine. 因此,这两种方法都可以正常工作。

The first approach will prevent the default action along with stopping event propagation.., use it only of that is what you want. 第一种方法将阻止默认操作并停止事件传播。.仅使用您想要的方法。

For the second approach to work, you need to accept the event parameter in the click handler like 对于第二种方法,您需要在点击处理程序中接受event参数,例如

$('.showSiteMap').on('click', function(e) {
    showSiteMap();
    e.preventDefault();
});

In both the approaches there is no need to have the href attribute like that, you can just use 在这两种方法中,都不需要像这样的href属性,您可以使用

<a href="#" class="deco-min showSiteMap">Site Map</a>

For links that aren't really links, what you put in the href is largely a matter of style. 对于不是真正链接的链接,您在href内容很大程度上取决于样式。 I like to give them hash fragments that suggest what they're going to do ( href="#sitemap" ), because users do sometimes look at the status area for a link to see what it's going to do.* But you can use href="#" , href="javascript:void(0)" , href="javascript:;" 我喜欢给他们一些散列片段,以提示他们要做什么( href="#sitemap" ),因为用户有时有时会在状态区域中查看链接以了解其操作。*但是您可以使用href="#"href="javascript:void(0)"href="javascript:;" , whatever. , 随你。

In your jQuery handler for the link, if you use a hash fragment, you may need to prevent the link's default action (following the link) if you don't want the hash to be added to the URL (and the page potentially scrolled to an element with the matching id , if any). 在链接的jQuery处理程序中,如果您使用哈希片段,则如果您不希望将哈希添加到URL(并且页面可能滚动到),则可能需要阻止链接的默认操作(跟随链接)具有匹配id的元素(如果有)。 You can do that with e.preventDefault() , or by using return false; 您可以使用e.preventDefault()或使用return false; . return false; does two things: e.preventDefault(); 件事: e.preventDefault(); and e.stopPropagation(); e.stopPropagation(); (preventing the event from bubbling to ancestor elements of the link). (防止事件从链接的冒泡到祖先元素)。 So use whichever makes sense for your application. 因此,请使用对您的应用程序有意义的任何一种。

So: 所以:

$('.showSiteMap').on('click', function() {
    showSiteMap();
    return false;
});

...if you want to prevent the default and stop propagation. ...如果要防止默认设置并停止传播。

Or: 要么:

$('.showSiteMap').on('click', function(e) {
    e.preventDefault();
    showSiteMap();
});

...if you just want to prevent the default (following the link) but not stop propagation. ...如果您只是想防止默认设置(遵循链接),但又不想停止传播。

If your href is either of the javascript: examples above, you don't have to prevent the default, because the default is a no-op. 如果您的href要么是中javascript:上面的例子,你不必阻止默认,因为默认是空操作。


* But I typically use the hash fragment. *但是我通常使用哈希片段。 Instead of handling a click on the link, I handle hash changes on the URL, and have the page change state to match the hash. 我不处理单击链接的问题,而是处理URL上的哈希更改,并使页面更改状态与哈希匹配。 This makes for a bookmarkable app. 这使得可收藏的应用程序成为可能。 (I don't use links for actions, that's what buttons are for.) (我不使用链接进行操作,这就是按钮的作用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM