在JavaScript中将`return false;`附加到`onClick`事件处理程序 [英] Append `return false;` to `onClick` event handler in JavaScript

查看:139
本文介绍了在JavaScript中将`return false;`附加到`onClick`事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要问题:当用户单击anchor标记时摆脱#.我找到了解决方案,在onClick处理程序中添加了return false;

Main Problem : Get rid of # when user clicks on anchor tag. I found a solution for this, which is adding return false; in onClick handler

次要问题:我当前的代码如下

<a href="#" onclick="javaScript:toggleDisplay();" title="Some Title">
    @ViewBag.TotalRecords
</a>

根据我得到的解决方案,我想要这样

And according to solution I got, I want it like this

<a href="#" onclick="javaScript:toggleDisplay();return false;" title="Some Title">
    @ViewBag.TotalRecords
</a>

  • 如何将return false;附加到我的javaScript函数调用中?我可以手动执行此操作,但是有没有简单的方法可以执行此操作.我正在使用jQuery吗?
  • 还有其他方法可以在不修改当前标记的情况下摆脱#吗?
    • How can I append return false; to my javaScript function call? I can do it manually, but is there any easy way to do this. I am using jQuery?
    • Is there any other way to get rid of #, without modifying the current markup?
    • 编辑

      @ViewBag.TotalRecords:这是MVC3 ASP.Net,但与问题无关,因此我没有将其放在标签中.

      @ViewBag.TotalRecords: This is a MVC3 ASP.Net thing, but it is not related to the question, hence I didn't put it in the tags.

      谢谢

      推荐答案

      感谢 Phil ,正如我所说的它的.不是只有一种解决方案,而是只有两种解决方案.

      Thanks Phil, as I said got something out of it. Got not one but two solutions.

      如果我将以下代码添加到js File中,则将处理所有click处理程序(稍后将使其专门针对anchor)

      If I add following code to js File, all click handlers are handled (will make it specific for anchor later on)

      使用纯JavaScript(不干扰JavaScript )

      Using plain JavaScript (Unobtrusive JavaScript)

      document.addEventListener('click', function (e) {
         e.preventDefault();
         return false;
      });
      

      使用jQuery

      $(document).click(function (e) {
          e.preventDefault();
          return false;
      });
      

      从某种意义上讲,如果我做了$('a').click(function(e){ e.preventDefault(); });,那么jQuery解决方案与 elclanrs 所建议的解决方案几乎没有什么不同. ,它适用于anchor,但是我修改了DOM并向其中添加了新的anchor,单击此锚点会导致查询字符串中的#.

      The jQuery Solution is little different than the one suggested by elclanrs, in the sense, if I do $('a').click(function(e){ e.preventDefault(); });, it works for anchor, but I modify my DOM and add new anchor to it, click on this anchor causes # in query string.

      编辑

      以上解决方案使所有点击都通过了处理程序,即使我不希望这样做.由于以href作为网址的正常anchor无法正常工作,因此这是有效的解决方案

      The above solution causes all the clicks to go through the handler, even which I don't want. Because of this normal anchor with href as url were not working, so this is the working solution

      $("a").live("click", function (e) {
         if ($(this).attr("href") != "#") return;
         e.preventDefault();
         return false;
      });
      

      希望这对其他人有帮助.

      Hope this helps some one else.

      这篇关于在JavaScript中将`return false;`附加到`onClick`事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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