gpt4 book ai didi

javascript - 使用javascript插入当前href

转载 作者:行者123 更新时间:2023-11-30 10:31:30 26 4
gpt4 key购买 nike

我正在编辑一个 Wordpress 插件,我需要在其中使用 Google Analytics 的事件跟踪 javascript 代码跟踪出站点击。挑战在于,我只能编辑一个产生多个链接的 php 文件。在下面的代码中,我只需要用该链接的 href 替换“this.getAttribute('href')”部分。如何才能做到这一点?我已经尝试了 this.getAttribute('href') 代码,但它没有执行。

<a href="http://lifeplace.com.au/" onclick="trackOutboundLink(this, 'Outbound Links', this.getAttribute('href')); return false;" title=""><img class="soliloquy-item-image" src="http://www.96five.com/wp-content/uploads/2013/03/image.jpg" alt="" title="Lifeplace"></a>

最佳答案

您可以随时将 href 动态设置为任何值:

var anchor=document.getElementsByTagName("a")[0]; // for the sake of the example
anchor.href="www.google.de";

http://jsfiddle.net/LP5dC/

关于javascript - 使用javascript插入当前href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16665460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com