简体   繁体   English

如何防止锚点href上的页面刷新

[英]How to prevent page refresh on anchor href

I am using the code below.我正在使用下面的代码。

<a ng-href="" ng-click="do()" eat-click>Click Me</a>
module.directive('eatClick', function () {
    return function (scope, element, attrs) {
        $(element).click(function (event) {
            event.preventDefault();
        });
    }
})

Is there a better way to do this?有一个更好的方法吗? Please share.请分享。 Thanks in advance.提前致谢。

event.preventDefault() , works properly in this case. event.preventDefault() ,在这种情况下正常工作。 You can also use ng-href tag of angular for alternative solution.您还可以使用 ng-href angular 标签作为替代解决方案。

$(element).click(function(event) {
    event.preventDefault();
});

You can also try below:-您也可以尝试以下:-

$('a[href=""]').click(function(event) {
    event.preventDefault();
});

There is no need of defining directive, only ng-href="" is enough.不需要定义指令,只需要 ng-href="" 就足够了。 Just replace all href with ng-href.只需将所有 href 替换为 ng-href。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM