如何正确使用没有js的用户的javascript链接 [英] How to properly use javascript-links for users without js

查看:143
本文介绍了如何正确使用没有js的用户的javascript链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我不会在这里创建副本,但我真的不知道该找什么/找不到任何有用的东西。



我在我的页面上有几个链接(< a> ),它们触发了jquery click()处理程序,该处理程序将新页面加载到div而不重新加载。为了使这些链接可用于非JavaScript用户,我想将它们链接到同一页面(这就是为什么我使用s而不是任何div / span /按钮)。与此相关的问题是脚本无论如何都会重新加载,所以你可以说jaavascript被覆盖。



那么如何阻止浏览器使用链接作为javascript链接?或者什么是允许非JavaScript用户能够正确使用网站的正确解决方案?



我自己想出的唯一解决方案就是使用按钮,但我觉得这很笨,因为我必须使用表格,虽然我只通过 GET 提交1个变量,这只在链接本身中指定。感谢您的帮助:)

解决方案

对于您正在做的事情,返回 false 会像其他人一样回答,但要注意在jQuery事件处理程序中过度使用 return false;



通过调用 preventDefault()来停止点击操作可能会更好。它将阻止链接被跟踪,但不会阻止任何其他可能附加的处理程序。有关详细说明,请参见 jQuery事件:停止滥用return false 。 / p>

例如:

  $(a)。click(function) (事件){
//您的自定义代码
event.preventDefault();
});


I hope I'm not creating a duplicate here, but I just don't really know what to look for/didn't find anything useful yet.

I got several links (<a>) on my page that trigger the jquery click() handler, which will load a new page into a div without reload. To make those links available to non-javascript users, I want to link them to the same page (that's why I'm using s and not just any div/span/button). Propblem with that is that the script will reload anyway, so you can say jaavascript is overwritten.

So how do I prevent the browser using the link as a link with javascript? Or what's the correct solution to allow non-javascript users to be able to use the site properly as well ?

The only solution I could come up with by myself is just using buttons, but I find that quite bulky as I have to use a form although I only submit 1 variable via GET which is only specified in the link itself. Thanks for help :)

解决方案

For what you're doing, returning false will work as others have answered, but beware of overusing return false; in jQuery event handlers.

You would probably be better served by calling preventDefault() to stop the click action only. It will prevent the link from being followed, but not stop any other handlers that may be attached. See jQuery events: stop misusing return false for a detailed explanation.

example:

$("a").click(function(event) {
    // your custom code
    event.preventDefault();
});

这篇关于如何正确使用没有js的用户的javascript链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆