我不知道如何插入第二个链接。我希望每个单选按钮都指向不同的链接。
<script>
function myFunction() {
if(document.getElementById('slide1').checked) {
document.getElementById('myLink').href = "https://www.nationsprint.com/clients/ccpopv2/catalog.cgi";
}
}
</script>
<input checked="" type="radio" name="slider" id="slide1">
<input type="radio" name="slider" id="slide2" >
<a href="" onclick='myFunction()' id="myLink">MyLink</a>
最佳答案
您无法将多个 href
添加到一个元素。如果我理解正确的话,您想根据所选的 radio 输入更改 href
。这是:
function myFunction() {
if (document.getElementById('slide1').checked) {
document.getElementById('myLink').href = "https://www.nationsprint.com/clients/ccpopv2/catalog.cgi";
} else if (document.getElementById('slide2').checked) {
document.getElementById('myLink').href = "https://stackoverflow.com/";
}
alert("Current 'href' is: " + document.getElementById('myLink').href);
}
<input checked="" type="radio" name="slider" id="slide1">
<input type="radio" name="slider" id="slide2" >
<a href="" onclick='myFunction()' id="myLink">MyLink</a>
关于javascript - 如何向所选的 javascript 广播添加第二个链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48333217/