“[removed]void(0);” vs “return false” vs “preventDefault()”

前端 未结 8 1283
滥情空心
滥情空心 2020-11-28 19:44

When I want some link to not do anything but only respond to javascript actions what\'s the best way to avoid the link scrolling to the top edge of the page ?
I know sev

8条回答
  •  眼角桃花
    2020-11-28 20:12

    I think that I have seen as well javascript:; around as the web develops, is hard to keep track to the tricks that are available out there.. but this is mainly about accessability (besides javascript:void(0); ) and just a small correction is not javascript:void(0) but javascript:void(0); which means do nothing so pretty much as return false; although not sure if javascript:return false; does the same..

    I always use and would suggest to use javascript:void(0); for a couple of reasons.. in my humble opinion, of course.

    1.) I use it because of the same someone mentioned above.. href="#" is not appropriate as it might indicate going to the top and even in that case '#top' would be more adequate for that case. But also this can trigger something else in your code that makes use of # (hashes) so another reason is to avoid conflicts with other javascript that might be using #. And I tend to look for this when using a plugin for example, and replace them immediately.. href='#' to href='javascript:void(0);' or href='javascript:;'

    2.) If I want to re-use a function for a group of specific Anchor tags, I can call it with the selector on any attribute without worrying about detecting the click event and preventing the default action and I do it without even thinking of it as a development preference.

    3.) In most cases if you are doing link building using javascript:void(0); tries to make a link to not be followed as the old href= rel=nofollow so it avoid indexing links that are actions. I'm not so sure about this one merely because I heard that crawlers and robots can now read even Flash so would not be surprised if they can read javascript links

    4.) Referring from 2.) you can target on a class like and forget about preventing the click event default action by using a href="javascript:void(0);" and then targetting the class directly from the selector at the jQuery function.

            jQuery(function($)
            {
                //from the rel
                $('a[rel="-your-rel-id"]') ... off('click').on('click',function()
    
                //from the class
                $('a.-the-class') ... off('click').on('click',function()
    
                //from the id
    
                $('a#-the-id').off('click').on('click',function()
                {
                --do something with this link
            });
    
    }); 
    

    I rather feel more comfortable using the class as you can always do...

    $(a#-your-id).hasClass(-yourclass-)

    or any other interesting combination and affect many links.. so I really won't suggest to use the A as a selector solely..

    Normally what I see in here being suggested is this:

      jQuery(function($)
            {
                //from the rel
                $('a[rel="-your-rel-id"]').on('click',function(event)
                //do something
                //prevent the click as is passed to the function as an event
                event.preventDefault();
            });
    
    });
    

提交回复
热议问题