我收到错误:
SyntaxError: Unexpected token ILLEGAL [http://localhost/test/drop:13]
我的页面代码操作系统:
<!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="mini.js"></script>
<script language="javascript">
$(document).ready(function() {
$("#main_div").empty();
$("#drp_name option").each(function() {
$("#main_div").append("<div>"+ $(this).text() +"</div>")
});
});
</script>
</head>
<body>
<select name="drp_name" id="drp_name">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="main_div"></div>
</body>
</html>
我使用jquery
v1.7.2
将 jQuery 库添加到您的页面。
将其放入
<head>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
...
</head>
您忘记添加 JqueryScript
您可以简单地使用:
<script src="jquery-1.7.2.min.js"></script>
如果您下载 jquery 脚本并添加到您的项目中
您没有将任何内容传递给函数:
$("#drp_name option").each(function(this) {
$("#main_div").append("<div>"+ $(this).val() +"</div>")
});
注意我在函数中的 .each(...
之后添加了“this”当您从选项元素中提取值时,我还将 $(this).text() 更改为 $(this).val() 。
你添加
<script type="text/javascript" src="mini.js"></script>
但不要包含你的 jQuery 库 js
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
或者 jquery 库是否在您本地
<script type="text/javascript" src="SOURCE_TO_YOUR_LOCAL_FILE"></script>