2

我想要一段 jQuery 来获取每个链接的VIDMID,然后写入每个EDITurl。

<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 的原因

4

1 回答 1

1

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

演示

$('.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);
});
于 2013-07-16T10:03:20.657 回答