1

我想向我的 jQueryMobile 页面添加动态链接。这是我的代码:

<ul data-role="listview" data-inset="true" id="categoriesList">
            <li data-role="list-divider" data-theme="c"><h4>Categories</h4></li>
            <li><a href="#dinamic?x=7&y=8" data-ajax="false">dinamic</a></li>
            <li><a href="#banner" data-rel="dialog">to banner</a></li>
        </ul>

如何获取 x 和 y 变量的值?

4

1 回答 1

0
function getParams(url){
    var results = {};
    var ps = url.split('?');

    // get string after '?' sign. if no '?' sign, then return empty object
    if(ps.length > 1)
       ps = ps[1];
    else
       return results;

    // get parameters split with '&' and split each one by '=' to get
    // name-value pairs
    var parts = ps.split('&');
    for(var i = 0; i < parts.length; i++){
        var parts2 = parts[i].split('=');
        results[parts2[0]] = parts2[1];
    }
    return results;
}

例子:

<li><a id="example" href="#dinamic?x=7&y=8" data-ajax="false">dinamic</a></li>

JS:

var url = document.getElementById('example').getAttribute('href');
var params = getParams(url);
console.log(params.x);
console.log(params.y);

小提琴

于 2013-07-16T15:09:10.057 回答