我怎么可以拦截与jQuery的链接使用Ajax调用时? [英] How can I intercept a link with jQuery when using a ajax call?

查看:147
本文介绍了我怎么可以拦截与jQuery的链接使用Ajax调用时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会做一些愚蠢的事。但是,如果我有像一个正常的链接:

I might be doing something stupid. But if I have a normal link like:

<div id="changeMe"></div>
<a href="/Not/Intercepted" id="interceptMe">A link</a>

和我附上一个jQuery click事件的链接,像这样:

and I attach a jQuery click event to the link like so:

$('#interceptMe').click(function() {
  $('#changeMe').text('Changed');
  return false;
});

一切工作桃色。该网页确实的不可以重定向到/不/拦截,这是什么,我会觉得这是正确的。

Everything works peachy. The page does not get redirected to /Not/Intercepted, which is what I would think would be correct.

现在...

我喜欢1.产品$不用彷徨一个Ajax调用我的点击事件,现在我的网页会被错误地重定向到的页面,基本上覆盖了Ajax调用。

I introduct a ajax call like $.get to my click event and now my page will be incorrectly redirected to the page, which essentially overwrites the ajax call.

$('#interceptMe').click(function() {
  $.get('/Ajax/Call', goesIn, function(comesOut) {
  $('#changeMe').html(comesOut);
    }, "html");
  return false;
});

有没有一种方法,使jQuery的或javascript还是拦截的链接点击,因此它不会去在href页面?我想保持在href对于那些没有启用JavaScript的用户。 TIA!

Is there a way to make jQuery or javascript still intercept the link click so it does not go to the href page? I want to keep the href for those users that aren't enabling javascript. TIA!

推荐答案

而不是返回FALSE,使用....

instead of return false, use ....

$("#interceptMe").click(function(event){

    event.preventDefault();

    // Ajax here

    return false; //for good measure
});

<一个href="http://docs.jquery.com/Events/jQuery.Event#event.$p$pventDefault.28.29">http://docs.jquery.com/Events/jQuery.Event#event.$p$pventDefault.28.29

我有许多问题与IE浏览器尤其是不听返回false。显然,这样有其他<一href="http://coffeeandpaste.blogspot.com/2009/02/javascript-onclick-return-false-does.html">http://coffeeandpaste.blogspot.com/2009/02/javascript-onclick-return-false-does.html

I've had many issues with IE especially not listening to return false. Apparently so have others http://coffeeandpaste.blogspot.com/2009/02/javascript-onclick-return-false-does.html

这篇关于我怎么可以拦截与jQuery的链接使用Ajax调用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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