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

标签 javascript jquery html

如标题中所述,问题是这一切会影响什么。

代码运行良好,您真的希望看到 <a> 中的“href”属性发生的任何事情。标记更改为“iref”。

我这样做是为了稍后通过​​ jquery.load() 加载内容

我应该采取不同的做法吗?什么是“正确”的方法呢? 谷歌呢,它会影响谷歌机器人吗?我问这个是因为:如果没有打开 javascript,链接需要从 href 更改为 iref 并且偏离路线。那么机器人能不能跟着他们?

谢谢大家的回答。

There is a fiddle

<a class="top-nav animMainBox" href="/home.html">Home</a>
<a class="top-nav animMainBox" href="notathome.html">Not at home</a>
<a class="top-nav animMainBox" href="/contact.html">Contact</a>


<style type="text/css">
    a{margin:10px;}
</style>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){

        hrefToIref();

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

    });


    function hrefToIref() {
        $('.animMainBox').each(function(){
            var url = $(this).attr('href');
            $(this).attr('href','javascript:;').attr('iref',url);
        });
        $('.button').each(function(){
            var url = $(this).attr('href');
            $(this).attr('href','javascript:;').attr('iref',url);
        });
    }

    function loadNewBox($this,animate) {
        // loading and returning new boxes via 
        // var url = $this.attr('iref');
        // $(".wrapper").load(url+' .toggle.box',{noncache: new Date().getTime()}, function(response, status, xhr) {}
    }


</script>

最佳答案

Should I do this different?

是的,绝对是。 iref 属性看起来很无效。

Can robots follow them or not?

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

What about google, does it affect google robots?

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

What would be the "right" way to do it?

只需保留 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
    …
}

关于javascript - 通过 jquery 将 attr href 更改为其他内容。这一切有什么影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17488027/

相关文章:

javascript - Coffeescript 将客户端文件移植到具有全局变量的 Node

javascript - 'click' 上的 jQuery 在更改为 Bootstrap 表后未触发

javascript - 如何使tinyMCE编辑器在首次初始化后保留在我的jQgrid textarea formit表单中?

html - 如何实现类似 Arc90 的 Readability 或 Instapaper 的 html 页面洗涤器?

javascript - 如何通过 Javascript 更改 css 类样式?

PHP显示存储的时间数据,并在考虑不同时区的同时存储时间数据

javascript - Angular 单元测试 Controller

javascript - 如何使用 jquery 克隆 iframe?

HTML 超链接按钮

html - 使用基于屏幕分辨率的媒体查询从多个 css 文件中进行选择