小编Man*_*til的帖子

在不重新加载页面的情况下在url中附加参数

我正在使用以下代码将param附加到url.这工作正常但是当参数被添加到url中时,页面将被重新加载.我想在不重新加载页面的情况下使用此功能.

function insertParam(key, value)
{
    key = escape(key); value = escape(value);

    var kvp = document.location.search.substr(1).split('&');


    var i=kvp.length; var x; while(i--) 
    {
        x = kvp[i].split('=');

        if (x[0]==key)
        {
                x[1] = value;
                kvp[i] = x.join('=');
                    alert('sdfadsf');
                break;
        }
    }

    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&'); 
    //alert(document.location.href);
Run Code Online (Sandbox Code Playgroud)

}

我想在不重新加载页面的情况下向url添加多个参数:

  • TXT1
  • TXT2
  • txt3
  • 链接1
  • LINK2
  • LINK3

我想要网址:"..../search.php"

点击txt2后我想要网址:"..../search.php #t_2"

点击link2后我想要网址:"..../search.php#t_1&l_2"

javascript url jquery append pushstate

1
推荐指数
1
解决办法
9979
查看次数

标签 统计

append ×1

javascript ×1

jquery ×1

pushstate ×1

url ×1