捕获.click以获取URL [英] Capturing .click for URLs

查看:96
本文介绍了捕获.click以获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Javascript/Ajax比较陌生.

I am relatively new to Javascript/Ajax.

当用户单击某些URL时,我希望完全使用Javascript/jquery处理该单击.

When the user clicks on certain urls, I wish to handle the click entirely with Javascript/jquery.

<a class="row_edit" href="/sales_item/edit/{{ item.id }}"></a>

我所做的是向此html文件中添加一个javascript:

What I have done is to add a javascript to this html file:

   $(document).ready(function () {  
        $(".row_edit").click(row_edit);
    });

   function row_edit() {
      var url = $(this).attr("href") + "/";
      ...
   }

这很好用,每次我单击row_edit时,javascript都会触发并为我执行部分网站加载.但是在某些情况下,实际的链接似乎会触发(我看到部分站点被加载为整个屏幕).看来有比赛条件.

This works pretty well, every time I click on row_edit, the javascript is firing and doing the partial site loading for me. But there are cases that it seems the actual link would fire instead (I see the partial site loaded as the entire screen). It seems there is a race condition.

我认为确保仅触发javascript的最佳方法是将 href 的内容更改为#.这样,我可以确保网址本身不会消失,只有我的JavaScript能够触发.但是,然后我如何在 row_edit()中获取我的 url 值?

I think the best way to make sure only the javascript is firing would be to change the content of the href to #. That way I make sure the url by itself won't go anyway and only my javascript is firing. However how would I get my url value in the row_edit() then?

也许我在这里采用了错误的方法.你们如何解决这个问题?

Maybe I am taking a wrong approach here though. How do you guys solve this problem?

推荐答案

要确保仅触发javascript(取消点击的默认操作(导航)),您必须调用 preventDefault();.在点击处理程序中.

To make sure only the javascript is firing (Cancel the default action (navigation) of the click), you have to call the preventDefault(); in the click handler.

例如:

$(document).ready(function () {  
        $(".row_edit").click(row_edit);
    });

   function row_edit(event) {
      var url = $(this).attr("href") + "/";
      ...
      event.preventDefault();
   }

这篇关于捕获.click以获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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