jQuery在每个循环中更改每个子节点的链接

时间:2013-07-16 11:11:19

标签: jquery

我正在尝试获取每个CID的{​​{1}},并将每个.vehicleID附加到href.vehicle-actions

&VID=[CID]

在我的脑海里,这似乎很简单,但我似乎无法理解我做错了什么

的jQuery

<ul>
            <li><a href="./garage_vehicle.php?mode=add_vehicle"><span>Create a Vehicle</span></a></li>

            <li class="vehicle-view toggleVehicleButton"><a class="vehicleID" href="./garage_vehicle.php?mode=view_own_vehicle&amp;CID=6"><span>2014 Audi A4</span></a>
                <div class="vehicle-actions" style="width: 179px; display: none;">
                    <ul>
                        <li><a href="/board/garage_vehicle.php?mode=view_vehicle">View Vehicle</a></li>
                        <li><a href="/board/garage_vehicle.php?mode=edit_vehicle">Edit Vehicle</a></li>
                        <li><a class="deletevehicle" href="">Delete Vehicle</a></li>
                        <li><a href="/board/garage_modification.php?mode=add_modification">Add Modification</a></li>
                        <li><a href="/board/garage_premium.php?mode=add_premium">Add Insurance</a></li>
                        <li><a href="/board/garage_quartermile.php?mode=add_quartermile">Add Quartermile Time</a></li>
                        <li><a href="/board/garage_dynorun.php?mode=add_dynorun">Add Dynorun</a></li>
                        <li><a href="/board/garage_track.php?mode=add_lap">Add Lap</a></li>
                        <li><a href="/board/garage_service.php?mode=add_service">Add Service</a></li>
                        <li><a href="/board/garage_vehicle.php?mode=view_own_vehicle">Set Main Vehicle</a></li>
                    </ul>
                </div>
            </li>

            <li class="vehicle-view toggleVehicleButton"><a class="vehicleID" href="./garage_vehicle.php?mode=view_own_vehicle&amp;CID=7"><span>2008 Chevrolet Corvette</span></a>
                <div class="vehicle-actions" style="width: 179px;">
                    <ul>
                        <li><a href="/board/garage_vehicle.php?mode=view_vehicle">View Vehicle</a></li>
                        <li><a href="/board/garage_vehicle.php?mode=edit_vehicle">Edit Vehicle</a></li>
                        <li><a class="deletevehicle" href="">Delete Vehicle</a></li>
                        <li><a href="/board/garage_modification.php?mode=add_modification">Add Modification</a></li>
                        <li><a href="/board/garage_premium.php?mode=add_premium">Add Insurance</a></li>
                        <li><a href="/board/garage_quartermile.php?mode=add_quartermile">Add Quartermile Time</a></li>
                        <li><a href="/board/garage_dynorun.php?mode=add_dynorun">Add Dynorun</a></li>
                        <li><a href="/board/garage_track.php?mode=add_lap">Add Lap</a></li>
                        <li><a href="/board/garage_service.php?mode=add_service">Add Service</a></li>
                        <li><a href="/board/garage_vehicle.php?mode=view_own_vehicle">Set Main Vehicle</a></li>
                    </ul>
                </div>
            </li>
        </ul>

3 个答案:

答案 0 :(得分:1)

您查看a.vehicleID元素,但应查看其父li

您的代码可以简化。我会这样做:

$('.vehicleID').each(function(i, val){
    var CID = getURLParameter(this.href, 'CID');
    $('.vehicle-actions a', $(this).closest('li')).attr('href', function(_, href){
         if (href.indexOf("&VID")!=-1) return href; 
         return href + '&VID=' + CID);
    });
});

在简化中,我更喜欢this.href而不是$(this).attr('href'),我使用the version of the attr function taking a function as second parameter

答案 1 :(得分:0)

piece代码可能无效,

$('.vehicle-actions',this).each(function(i, val){
    editCID = $(this).find('a');
    editCID.attr('href', editCID.attr('href') + '&VID=' + CID);
});

$('.vehicle-actions',this)中,这不会识别下一个div.vehicle-actions,因为它不是a子项,因此请将其closest li.vehicle-view用作父选择器

试试这个,

$('.vehicle-actions',$(this).closest('li.vehicle-view')).each(function(i, val){
    editCID = $(this).find('a');
    editCID.attr('href', editCID.attr('href') + '&VID=' + CID);
});

答案 2 :(得分:0)

您尚未在所有代码中指定vehicleID,因此以下语句将仅返回一个元素。

$('.vehicleID').length

所以在所有标签中正确添加类。