无法使用jquery将新网址加载到href属性

时间:2014-04-07 13:21:57

标签: jquery

我使用jquery使用$ .each函数更改所有锚标记的href属性。 我可以在控制台中获取新的href,但是当点击元素时它不会重定向到更改的新URL。

$(".section-icons").each(function () {
    var newHref = $(this).find("a").attr("href");
    newHref += "?currentMod=sales_target";
    console.log(newHref);
    $(this).find("a").attr("href", newHref);
});

<div class="section-icons">
    <a href="sales-target-revenue-wise.php">
        <div class="modules-icons-dd">
            <img src="<?=$dir_path?>images/module-icons/sales-target-revenue-wise-icon.png" />
        </div>
        <div class="section-text">Revenue Wise Sales Target</div>
    </a>
</div>

1 个答案:

答案 0 :(得分:2)

这样做:

$(document).ready(function () {
    $(".section-icons").each(function () {
        var newHref = $(this).find("a").attr("href");
        newHref += "?currentMod=sales_target";
        console.log(newHref);
        $(this).find("a").attr("href", newHref);
    });
});

你需要在DOM准备好之后这样做。

此外,您的脚本需要包含在<script></script>

Demo