【问题标题】:How to append query string arguments to hyperlink URL using jQuery如何使用 jQuery 将查询字符串参数附加到超链接 URL
【发布时间】:2013-01-05 05:35:10
【问题描述】:

我有一个完整的 jQuery 代码来选择与指定域匹配的 URL:

jQuery( document ).ready( function( $ ) {
    $('a').each(function() {

        var href = $(this).attr('href');


        if(this.hostname && this.hostname == 'example.com') 
        {                   
            $(this)
                .removeAttr('target')
                .attr('rel', 'nofollow')                
                .attr('title', href)
       }
    });
});

如您所见,它将删除目标属性,添加 rel nofollow 并将标题设置为 URL 值。现在我遇到了关于如何修改上述代码以添加另一个功能以将查询字符串附加到 URL 值(href 值)的问题。假设我想附加以下查询字符串参数/值:

argument1='test1'
argument2='test2'

这样最终的 URL 将如下所示:

http://example.com/?argument1=test1&argument2=test2

或示例域的任何页面,如

http://example.com/any_page/?argument1=test1&argument2=test2

有没有不使用jQuery插件的简单方法?

【问题讨论】:

  • 在脚本中添加nofollow 并不能真正达到你想要的效果。谷歌等人运行了一些 JS,但不多,你的 nofollow 可能不会被尊重。最好只在服务器端完成这项工作。
  • 谢谢克林特,我明白了。但是任何想法如何将查询字符串参数添加到选定的超链接 URL?

标签: jquery


【解决方案1】:

查看jQuery.param() 了解对象和数组

.serialize() 获取表单数据

例如:http://jsfiddle.net/DqXYn/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多