weixin_33686714 2012-09-03 09:25 采纳率: 0%
浏览 57

捕获.click以获取URL

I am relatively new to Javascript/Ajax.

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>

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") + "/";
      ...
   }

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.

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?

  • 写回答

3条回答 默认 最新

  • MAO-EYE 2012-09-03 09:29
    关注

    I am not sure about the intent of performing the redirection using javascript instead of href. Anyways, one of the approaches is here...

    If you are implementing for modern browsers which support "data-" (HTML5) then you can use such attributes

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

    and modify the javascript to get the appropriate attribute value

     var url = $(this).attr("data-url") + "/";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作