gpt4 book ai didi

javascript - 添加带有属性的新选择选项

转载 作者:行者123 更新时间:2023-11-28 17:51:55 24 4
gpt4 key购买 nike

我想将 href 属性添加到我的选择列表 (CountryList) 元素中:

var s = document.getElementById('CountryList');

var option = document.createElement("option");
option.text = "FR";
option.value = "FR";
option.setAttribute('href', 'https://www.facebook.com/');
s.add(option);

var option2 = document.createElement("option");
option2.text = "EN";
option2.value = "US";
option2.setAttribute('href', 'https://www.facebook.com/');
s.add(option2);

var option3 = document.createElement("option");
option3.text = "AR";
option3.value = "AR";
option3.setAttribute('href', 'https://www.google.com/');
s.add(option3);

var option4 = document.createElement("option");
option4.text = "ES";
option4.value = "ES";
option4.setAttribute('href', 'https://www.yahoo.com/');
s.add(option4);

var option5 = document.createElement("option");
option5.text = "IT";
option5.value = "IT";
option5.setAttribute('href', 'https://www.youtube.com/');
s.add(option5);

当我选择一个选项时,它应该将我重定向到特定的 href 地址,但目前它只是将我重定向到“yahoo”和“youtube”,而其他元素不会重定向。

最佳答案

定义var s=document...后添加以下内容:

s.onchange = function() {
var sel = this.options.selectedIndex;
window.location = this.options[sel].getAttribute('href');
}

关于javascript - 添加带有属性的新选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45315957/

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