Javascript 为什么这个脚本不起作用而且。。。?

Javascript 为什么这个脚本不起作用而且。。。?,javascript,Javascript,html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/ht

html代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="script.js">
</script>
</head>

<body bgcolor="#FFFFCC">
<center>
<form>
 <select id="newLocation">
  <option value="1.jpg">index</option>
   <option value="script.js">posts</option>
   <option value="2.jpg" selected="selected">blog</option>
 </select>
</form>
当我从组合框中选择一个选项时,为什么不转到新页面,即值? 还有document.getElementByIdnewLocation;此语句是函数jumpPage return的第一个语句?

您可以尝试

var newPage = newLoc.options[newLoc.selectedIndex].value;
声明

var newLoc = document.getElementById("newLocation"); 
只查找DOM HTML元素,即您的。

文档。getElementByIdnewLocation将返回选择对象,即对下拉列表的引用


关于JS,它有一个错误。您应该将newLoc.getSelectedIndex更改为newLoc.selectedIndex。

诊断此类问题的常用方法是调试。使用console.log或alert进行测试输出,以查看哪一点出现了错误。通过这种方式,您还可以自己找到该行返回的内容,您正在询问@Pekkai是否尝试过该方法,但不理解输出。它返回对象HTML select元素。我不明白这一点。如果你真的想学习如何做这件事,也许可以问一个问题。这将比有人找出当前剧本中的问题要更有成效。什么是不理解的?如果按ID获取元素,则将获取该元素。
var newLoc = document.getElementById("newLocation");