首页 > 解决方案 > Jquery 使用每个替换具有特定 ID 的某些链接

问题描述

有人能告诉我为什么这个 jquery 不起作用吗?

<script type="text/javascript">
        jQuery('a').each(function(){
            jQuery("#get-pricing-now-button-hotfix8").attr("href", "/en/starting-prices/");
        });
    </script>

我在页面上有几个按钮,它们的 id 值为 get-pricing-now-button-hotfix8,我需要将这些链接的 ahref 值切换到 /en/starting-prices/。

我能够使用以下方法使其适用于一个链接,但仅更改 1 个按钮。

  <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery("#get-pricing-now-button-hotfix8").attr("href","/en/starting-prices/");
        });
    </script>

请帮忙!

标签: jquery

解决方案


感谢@cloned 为我指明了正确的方向。

最后,我使用了这段代码和新 URL 的任何实例。

我将 #get-pricing-now-button-hotfix8 更改为 .get-pricing-now-button-hotfix8 (从 ID 更改为类)

  <script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery(".get-pricing-now-button-hotfix8").attr("href","/en/starting-prices/");
    });
</script>

推荐阅读