1

我正在使用此功能通过选择更改按钮处的链接,现在我想用另一个按钮添加另一个选择,但我只能通过两次添加相同的功能并更改一个名称来做到这一点。有什么提示吗?

changelink(s) {
    if(s.options[0].selected){
        document.getElementById('yahoo').style.visibility = 'hidden';
        document.getElementById('google').style.visibility = 'hidden';
        document.getElementById("wsj").style.visibility = 'visible';
    }
    if(s.options[1].selected){
        document.getElementById('yahoo').style.visibility = 'hidden';
        document.getElementById('google').style.visibility = 'visible';
        document.getElementById('wsj').style.visibility = 'hidden';
    }
    if(s.options[2].selected){
        document.getElementById('yahoo').style.visibility = 'visible';
        document.getElementById('google').style.visibility = 'hidden';
        document.getElementById('wsj').style.visibility = 'hidden';
    }
}
4

1 回答 1

0

如果我理解正确,应该这样做。我只是将 ID 更改为类,并在changelink函数中添加了一个参数来确定使用哪个以及影响select哪个a

/* HTML */
<select onchange="changelink(this, 0)">
    <option value="0">3 mesi</option>
    <option value="1">6 mesi</option>
    <option value="2">9 mesi</option>
</select>
<a class="google" href="http://www.google.com">a</a>
<a class="yahoo" href="http://www.yahoo.com">b</a>
<a class="wsj" href="http://www.wsj.com">c</a>
<select onchange="changelink(this, 1)">
    <option value="0">3 mesi</option>
    <option value="1">6 mesi</option>
    <option value="2">9 mesi</option>
</select>
<a class="google" href="http://www.google.com">a</a>
<a class="yahoo" href="http://www.yahoo.com">b</a>
<a class="wsj" href="http://www.wsj.com">c</a>

/* Javascript */
function changelink(s, i) {
    if(s.options[0].selected){
        document.getElementsByClassName('yahoo')[i].style.visibility = 'hidden';
        document.getElementsByClassName('google')[i].style.visibility = 'hidden';
        document.getElementsByClassName("wsj")[i].style.visibility = 'visible';
    }
    if(s.options[1].selected){
        document.getElementsByClassName('yahoo')[i].style.visibility = 'hidden';
        document.getElementsByClassName('google')[i].style.visibility = 'visible';
        document.getElementsByClassName('wsj')[i].style.visibility = 'hidden';
    }
    if(s.options[2].selected){
        document.getElementsByClassName('yahoo')[i].style.visibility = 'visible';
        document.getElementsByClassName('google')[i].style.visibility = 'hidden';
        document.getElementsByClassName('wsj')[i].style.visibility = 'hidden';
    }
}

下次您应该包含一些 HTML 或您目前拥有的内容,以便我们更好地了解您希望它如何工作

于 2013-08-30T15:37:56.183 回答