我不是 Javascript 的金星奖章,所以请耐心等待。
我们在网站上添加了一个通过 javascript(称为费率卡)从外部加载的内容,但是提供的该项目正在使用的链接不正确,因此我们正在尝试使用一点 javascript/jquery覆盖它。
当插件加载时,它会向预定义的 div 添加一个 location.href。我正在尝试通过以下 JavaScript 覆盖它。
这是正在生成的div
<div class="wdgt_widget" style="border:1px solid #1d8be0 !important; border-radius:8px !important; padding:11px !important; width:300px !important; height:64px !important; cursor:pointer !important; box-sizing: border-box !important;" onclick="location.href='https://ratecard.io/staffing-ms'"></div>
所以我尝试先解除点击操作的绑定(bind)并添加一个新的点击操作,但什么也没有发生。请参阅下面的脚本。
<script>
jQuery(document).ready(function ($) {
setTimeout(function() {
$('#wdgt_widget').unbind();
$('#wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/");
}, 5000);
});
</script>
我在上面添加了一个 5 秒计时器,因为我想确保代码会被加载并且 div 已经生成。
遗憾的是,什么也没发生。是否有可能简单地在 onclick 上使用替换?或者我在这里做错了什么?
最佳答案
问题:
1)这个$('#wdgt_widget')
您在选择器中使用#
,用于按id进行选择。但是您的 div
中有一个类,因此请使用 .
解决方案: 1) 将上述语法替换为 $('.wdgt_widget')
问题: 2) attr 函数末尾缺少 '
,并且 click 之间缺少
和,
位置
。
$('#wdgt_widget').attr('click' 'location.href="https://ratecard.io/staffing-ms/review/");//和 ' 缺失。
解决方案: 2) 更改 .attr('click' 'location.href="https://ratecard.io/staffing-ms/review/")
到 .attr('click','location.href="https://ratecard.io/staffing-ms/review/"')
请注意,我在中间添加了 ,
,并在最后添加了 '
。
关于javascript - 通过 jquery 将加载的 url 替换为另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40888773/