jQuery读取查询字符串 [英] Jquery read query string
本文介绍了jQuery读取查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在用Jquery读取查询字符串时遇到麻烦.我想做的就是读取查询字符串,并根据其显示的内容滚动到某个位置或元素.
I'm having trouble reading a query string with Jquery. What I want to do is read the query string and based on what it says scroll to a certain place or element.
这是我的代码
$(document).ready(function () {
var name = getQueryParam("id")
{ var pairs = location.search.substring(1).split('&'); for (var i = 0; i < pairs.length; i++)
{ var params = pairs[i].split('='); if (params[0] == param) { return params[1] || ''; } } return undefined; }; })(jQuery);
if ( name == 1){
scrollTo(0, 800);
}
});
推荐答案
您可以使用此函数获取查询字符串值:
You can use this function to get query string value:
function getParameterByName( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
示例:
var param = getParameterByName('yourVar');
这篇关于jQuery读取查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文