gpt4 book ai didi

javascript getElementsByTagName

转载 作者:行者123 更新时间:2023-11-28 01:32:16 27 4
gpt4 key购买 nike

问题是创建一个函数 showLink(),它有一个变量 sIndx,该变量指向当前选择列表中所选选项的索引。

   <select name="executive" id="executive" class="optionLinks">
<option value="#">Select a Web site</option
<option value="http://www.whitehouse.gov">The White House</option>
<option value="http://www.usda.gov">Department of Agriculture</option>
</select>
<select name="legislative" id="legislative" class="optionLinks">
<option value="#">Select a Web site</option>
<option value="http://www.house.gov">House Web Site</option>
<option value="http://www.house.gov/house </option>
<option value="http://clerk.house.gov/">Clerk of the House</option>
</select>
<select name="judicial" id="judicial" class="optionLinks">
<option value="#">Select a Web site</option>
<option value="http://www.uscourts.gov">U.S. Courts</option>
<option value="http://www.uscourts.gov/supremecourt.html">U.S.Supreme </select>

我的回答:

function Link()  {   
sIndex = document.getElementByTagName("select")[this.select].options[selectedIndex];
location.href = sindex.value;
}

网页未加载,我仍然看到单击的选项,有什么帮助吗?

最佳答案

您输错了函数名称,它应该是 document.getElementsByTagName(复数“Elements”)。

函数 document.getElementsByTagName("select") 将返回一个 select 对象数组。要获取正确的对象,您需要知道其在数组中的正确索引。您正在使用 this.select 来实现此目的,请确保检查它是否具有正确的值。

即使这有效,我也看不到您在代码中定义 selectedIndex 变量的位置。也许你的意思是这样的:

function Link() {
var obj = document.getElementByTagName("select")[this.select];
// assuming this ^^^^^^^^^^^ is the right index
var item = obj.options[obj.selectedIndex];
location.href = item.value;
}

您的 HTML 中也存在拼写错误,在 #legislative 中,您没有正确关闭 value 属性,这可能会导致问题。

关于javascript getElementsByTagName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21976779/

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