Javascript 通过jquery将attr href更改为其他内容。这一切会影响什么

Javascript 通过jquery将attr href更改为其他内容。这一切会影响什么,javascript,jquery,html,Javascript,Jquery,Html,正如标题中提到的,这一切会影响什么 代码运行良好,除了 a{margin:10px;} $(文档).ready(函数(){ hrefToIref(); $(document).on('click','animMainBox',function(){ loadNewBox($(此),true); }); }); 函数hrefToIref(){ $('.animMainBox')。每个(函数(){ var url=$(this.attr('href'); $(this.attr('href','j

正如标题中提到的,这一切会影响什么

代码运行良好,除了


a{margin:10px;}
$(文档).ready(函数(){
hrefToIref();
$(document).on('click','animMainBox',function(){
loadNewBox($(此),true);
});
});
函数hrefToIref(){
$('.animMainBox')。每个(函数(){
var url=$(this.attr('href');
$(this.attr('href','javascript:;')).attr('iref',url);
});
$('.button')。每个(函数(){
var url=$(this.attr('href');
$(this.attr('href','javascript:;')).attr('iref',url);
});
}
函数loadNewBox($this,animate){
//通过
//var url=$this.attr('iref');
//$(“.wrapper”).load(url+”.toggle.box“,{noncache:new Date().getTime()},函数(响应,状态,xhr){}
}

为什么不这样做:

$(document).ready(function(){

        $('a').click(function(){ return false; });

        $(document).on('click','.animMainBox',function(){
            loadNewBox($(this), true);
        });

    });
这将停止链接的工作,您可以稍后加载内容。
搜索机器人仍然可以访问链接是的

我应该做不同的事情吗

当然可以。
iref
属性看起来非常无效

机器人能否跟随它们

是的,他们通常只查看带有
href
属性的静态HTML标记

谷歌呢,它会影响谷歌机器人吗

我认为谷歌有点不同,因为他们可以在打开JS的情况下查看页面。我不知道你的脚本是否会阻止他们跟随链接

“正确”的方法是什么

只需保持
href
属性的原样。在单击属性时防止跟随它们

$(document).on('click','.animMainBox, .button', function(e){
    e.preventDefault();
    loadNewBox($(this), true);
});
function loadNewBox($this,animate) {
    var url = $this.attr('href');
    //                    ^ just use the correct attribute
    …
}

@Connor问得好。如果我翻译effect和/或affect似乎都是对的。所以我真的不知道哪一个更好。我认为affect听起来更有效,但几乎不起作用,因为
return false
将导致
stopPropagation()
。您可以在
$('a')中添加
load
函数。单击(function()){return false;});
。是的,你可以。但是你这样做会失去活动授权。非常感谢你的时间和解释!
$(document).on('click','.animMainBox, .button', function(e){
    e.preventDefault();
    loadNewBox($(this), true);
});
function loadNewBox($this,animate) {
    var url = $this.attr('href');
    //                    ^ just use the correct attribute
    …
}