简体   繁体   中英

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. You can also use ng-href tag of angular for alternative solution.

$(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. Just replace all href with ng-href.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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