gpt4 book ai didi

javascript - 如何替换 Javascript 中的 href 值

转载 作者:行者123 更新时间:2023-11-30 17:40:18 25 4
gpt4 key购买 nike

我有以下代码根据点击的缩略图更改大图像(这部分工作)。

我想相应地更改 href 中的 url(以便显示的每个大图像都有指向另一个 URL 的链接)。 <-- 最后一部分不起作用。

------------这是代码--------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>image swapping </title>



<script type="text/javascript">

function init(){

th=document.getElementById('thumbs').getElementsByTagName('img');
for(c=0;c<th.length;c++) {
th[c].onclick=function() {
swapImage(this.src);
swapFile(this.href);

}
}
}

function swapImage(url){

str=url.lastIndexOf(".");
str1=url.substring(str-1);
str2=url.substring(str);

url=url.replace(str1,str2);

document.getElementById('bigpic').src=url;

}

function swapFile(url){

str=url.lastIndexOf(".");
str1=url.substring(str-1);
str2=url.substring(str);

url=url.replace(str1,str2);

document.getElementById('file').href=url;

}

window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);

</script>

</head>
<body>

<div>
<a href="01.html" id="file"><img id="bigpic" src="images/01.jpg" alt=""></a>
</div>

<div id="thumbs">
<img src="images/01t.jpg" alt="01t.jpg">
<img src="images/02t.jpg" alt="02t.jpg">
<img src="images/03t.jpg" alt="03t.gif">
<img src="images/04t.jpg" alt="04t.png">
<img src="images/05t.jpg" alt="05t.png">
<img src="images/06t.jpg" alt="06t.png">
</div>

</body>
</html>

最佳答案

这个问题可能会被标记为重复问题,但无论如何这就是答案。

更改 anchor 的 href 值:

document.getElementById('file').setAttribute('href',url);

关于javascript - 如何替换 Javascript 中的 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21223315/

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