<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head><title></title>
<script language="javascript">
<!--
window.onload=m;
function m()
{ var e=document.getElementById("e1");
var s=createElement("select");
var o=createElement("option");
s.lastChild(o);
var t=createTextNode("jjjjjjjjj");
o.appendChild(t);
o.setAttribute("value","30");
}
//-->
</script>
</head>
<body>
<div id="e1"></div>
</body>
</html>
<html>
<meta charset="utf-8" />
<head><title></title>
<script language="javascript">
<!--
window.onload=m;
function m()
{ var e=document.getElementById("e1");
var s=createElement("select");
var o=createElement("option");
s.lastChild(o);
var t=createTextNode("jjjjjjjjj");
o.appendChild(t);
o.setAttribute("value","30");
}
//-->
</script>
</head>
<body>
<div id="e1"></div>
</body>
</html>
解决方案 »
- 请问在FCKeditor中,下面代码的FCKeditor1是指什么对像?
- new Function 返回值怎么是undefined?
- 提交表单 onClick _blank
- ie7禁用了图片本地预览,那么怎样解决呢?
- js里如何将字符类型转化为数值类型
- 为什么添加了function updateSales()之后连js里第一个alert都不显示?
- 这个功能能实现么?
- 关于动态表单的java
- 哪位老兄可以帮忙解码下面这段js的,不胜感激,可用送分30
- js做的网页围棋
- 想要鼠标点击a或b或c..中的一个元素,然后top元素就移动到这个点过的元素周边怎么写呢?
- EXTJS中TreePanel和TabPanel配合使用的问题
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<div id="e1"></div>
<script language="javascript">
<!--
window.onload=m;
function m()
{
var dom_select = document.createElement('select');
dom_select.name = 's1';
dom_select.id = 's2';
var s1_option = document.createElement('option');
dom_select.appendChild(s1_option);
var s1_option_text = document.createTextNode('node');
s1_option.appendChild(s1_option_text);
document.getElementById('e1').appendChild(dom_select);
}
//-->
</script>
</body>
</html>
var opt = document.createElement("option");
var _text = document.createTextNode("我是text"); //创建文本
opt.appendChild(_text); //把文本赋给标签
opt.setAttribute("value",'aaa'); //给option赋value值
opt.setAttribute("selected","selected");
//添加
selectCmp.appendChild(opt);
document.body.appendChild(selectCmp);
第一,s1_option 的value属性至少得设上吧,于是我加了一句
s1_option.setAttribute("value","30);
第二,在《javascript DOM编程艺术》中说要像使用CSS一样用DOM,所以我为了把HTML和它分离开
就把脚本内容移到<head>里去了;
以上两步做完,就又不显示了,唉,我到底错在哪呢?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
<!--
window.onload=m;
function m()
{
var dom_select = document.createElement('select');
dom_select.name = 's1';
dom_select.id = 's2';
var s1_option = document.createElement('option');
dom_select.appendChild(s1_option);
var s1_option_text = document.createTextNode('node');
s1_option.appendChild(s1_option_text);
s1_option.setAttribute("value","30); document.getElementById('e1').appendChild(dom_select);
}
//-->
</script></head><body>
<div id="e1"></div></body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
<!--
window.onload=m;
function m()
{
var dom_select = document.createElement('select');
dom_select.name = 's1';
dom_select.id = 's2';
var s1_option = document.createElement('option');
dom_select.appendChild(s1_option);
var s1_option_text = document.createTextNode('node');
s1_option.appendChild(s1_option_text);
s1_option.setAttribute("value","30"); document.getElementById('e1').appendChild(dom_select);
}
//-->
</script></head><body>
<div id="e1"></div></body>
</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>无标题文档</title>
<script language="javascript">
<!--
var selectCmp = document.createElement("select");
var opt = document.createElement("option");
var _text = document.createTextNode("我是text"); //创建文本
opt.appendChild(_text); //把文本赋给标签
opt.setAttribute("value",'aaa'); //给option赋value值
opt.setAttribute("selected","selected");
//添加
selectCmp.appendChild(opt);
document.body.appendChild(selectCmp);
//-->
</script></head><body>
<div id="e1"></div></body>
</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>无标题文档</title>
<script language="javascript">
<!--
window.onload=m;
function m()
{
var dom_select = document.createElement('select');
dom_select.name = 's1';
dom_select.id = 's2';
for(var i=1;i<100;i++)
{
s1_o=document.createElement('option');
dom_select.appendChild(s1_o);
s1_o_t=document.createTextNode(i);
s1_o_t.setAttribute("value",i); //这句有毛病,但不知错在哪
s1_o.appendChild(s1_o_t); }
document.getElementById('e1').appendChild(dom_select);
}
//-->
</script></head><body>
<div id="e1"></div></body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
<!--
window.onload=m;
function m()
{
var dom_select = document.createElement('select');
dom_select.name = 's1';
dom_select.id = 's2';
for(var i=1;i<100;i++)
{
s1_o=document.createElement('option');
dom_select.appendChild(s1_o);
s1_o_t=document.createTextNode(i);s1_o.setAttribute("value",i); //这句有毛病,但不知错在哪
s1_o.appendChild(s1_o_t);}
document.getElementById('e1').appendChild(dom_select);
}
//-->
</script></head><body>
<div id="e1"></div></body>
</html>变量写错了,应该加在s1_o上