javascript getElementByTagName
问题是创建一个函数javascript getElementByTagName,javascript,variables,Javascript,Variables,问题是创建一个函数showLink(),该函数有一个变量sIndx,该变量指向当前选择列表中所选选项的索引 <select name="executive" id="executive" class="optionLinks"> <option value="#">Select a Web site</option <option value="http://www.whitehouse.gov">The White Hou
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>
网页未加载,我仍然看到选项已单击,是否有帮助?您键入的函数名错误,它应该是
document.getElementsByTagName
(复数“元素”)
函数document.getElementsByTagName(“选择”)
将返回一个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中还有一个输入错误,在#legital
中,您没有正确关闭值
属性,这可能会导致问题
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;
}