【问题标题】:unable to load the new url to the href attribute using jquery无法使用 jquery 将新 url 加载到 href 属性
【发布时间】:2014-04-07 21:24:51
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    这样做:

    $(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 准备好之后再做。

    另外,您的脚本需要包含在&lt;script&gt;&lt;/script&gt;

    Demo

    【讨论】:

    • 我可以这么说是因为他没有将其包含在问题中。为什么头脑正常的人会剥离他们的代码并只将部分代码粘贴到 SO 上?
    • @amit true。这东西效果很好。但是当还使用引导程序时,同样的方法也不起作用:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多