jquery - 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>
请帮忙!
解决方案
感谢@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>
推荐阅读
- jitsi - React Native + lib-jitsi-meet - 音频/视频轨道未添加到会议中
- javascript - 禁用 next.js 服务器端渲染以防止窗口未定义错误
- swift - Swift 中 OperationQueue 上可用但不可用的函数
- excel - Excel VBA:在空白单元格中从上面的单元格复制数据,但仅在 AB 列中
- amazon-athena - Athena:不支持这种类型的查询
- elasticsearch - Elasticsearch 查询可以有空值参数吗?
- r - 将每组的某些行的中位数除以其他行的中位数
- javascript - 如何为不同的“正确”答案分配要汇总的单个值?
- javascript - 具有不同 ExponentIndicator 的 Javascript parseFloat
- javascript - 加拿大邮政地址需要完整的 API 使用帮助