jQuery获取每个URL的VID和MID并建立链接

时间:2013-07-16 09:47:31

标签: jquery

我想有一段jQuery来获取每个链接的VIDMID,然后写入每个EDIT网址。

<ul>
    <li><a href="garage_modification.php?mode=view_modification&VID=6&MID=11" class="item">Induction Kit</a> - <a href="garage_vehicle.php?mode=edit_modification" class="edit">Edit</a></li>
    <li><a href="garage_modification.php?mode=view_modification&VID=6&MID=12"class="item">Induction Kit</a> - <a href="garage_vehicle.php?mode=edit_modification" class="edit">Edit</a></li>
</ul

它基本上就像

<ul>
    <li><a href="garage_modification.php?mode=view_modification&VID=6&MID=11" class="item">Induction Kit</a> - <a href="garage_vehicle.php?mode=edit_modification&VID=6&MID=11" class="edit">Edit</a></li>
    <li><a href="garage_modification.php?mode=view_modification&VID=6&MID=12"class="item">Induction Kit</a> - <a href="garage_vehicle.php?mode=edit_modification&VID=6&MID=12" class="edit">Edit</a></li>
</ul>

生成的链接将是动态的,这就是我想要使用jQuery

的原因

1 个答案:

答案 0 :(得分:1)

一个简单的解决方案是解析href并提取VID和MID值,然后使用.next()将元素附加到以下值:

Demo

$('.item').each(function(i, val){
    var VID = '', MID = '', param, editAnchor;
    var parts = val.href.substr(val.href.indexOf('?')+1).split('&');
    $.each(parts, function(ind, part){
        param = part.split('=');
        switch(param[0]){
            case 'VID': VID = param[1]; break;
            case 'MID': MID = param[1]; break;
        }
    });
    editAnchor = $(this).next('.edit');
    editAnchor.attr('href', editAnchor.attr('href') + '&VID=' + VID + '&MID=' + MID);
});