我有以下脚本:
<script>
window.onload = function setHref(){
var affglcid = <?php echo json_encode($kws); ?>;
var oldLink=document.getElementById('link').href;
document.getElementById('link').setAttribute('href', oldLink+affglcid);
var oldLink1=document.getElementById('link2').href;
document.getElementById('link2').setAttribute('href', oldLink1+affglcid);
}
</script>
和:
<a id="link" href="oursite.com/">Link</a>
<a id="link2" href="othersite.com/">Link</a>
首先,它需要一个PHP变量:
var affglcid = <?php echo json_encode($kws); ?>;
然后,它将变量附加到链接的末尾:
var oldLink=document.getElementById('link').href;
document.getElementById('link').setAttribute('href', oldLink+affglcid);
它应该对另一个链接执行相同的操作:
var oldLink1=document.getElementById('link2').href;
document.getElementById('link2').setAttribute('href', oldLink1+affglcid);
因此,如果 $kw=xy,那么第一个链接应该是"oursite.com/xy",第二个链接应该是"othersite.com/xy",但它仅适用于另一个站点链接。用于此的代码如下:
<a id="link" href="oursite.com/">Link</a>
<a id="link2" href="othersite.com/">Link</a
任何想法出了什么问题?
我注意到您的URL不正确,它们应该以"http://"或"https://"开头
<a id="link" href="http://oursite.com/">Link</a>
<a id="link2" href="http://othersite.com/">Link</a>
即使你不想添加协议,你至少应该使用//oursite.com,否则它会被视为相对 URL,它会将此 URL 附加到您当前 URL 的前面并尝试打开它