Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript getElementByTagName_Javascript_Variables - Fatal编程技术网

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;
}