jQueryMobile中的动态链接

时间:2013-07-16 14:54:11

标签: javascript jquery-mobile url-parameters

我想添加动态链接到我的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变量的值?

1 个答案:

答案 0 :(得分: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);

FIDDLE