<html>
<head>
<title></title>  
<script>
//程序的作用是,先在页面上生成一个按键,这个按钮一按下去就再生成一个下拉框。function writeDiv(context,divId)
{
var div = document.getElementById(divId);
div.innerHTML = context; 

}
function test()
{ tmp="<select name='select23' id='select23' onchange=\"selectChange('select23');\"><option value='1'>市场部<\/option><option value='2'>技术部<\/option><\/select> ";
var s="<INPUT TYPE=\"button\" value=\"Create\" onclick=\""+tmp+";\">";
writeDiv(tmp,"test");
} </script>
</head>
<body>
<div id="test">
<script>
test();
</script>
  
</body></html>

解决方案 »

  1.   

    tmp="writeDiv(\"<select ....
    这是什么意思?
      

  2.   

    楼上的怎么把我的要求改了?我刚才的代码贴错了,应该这样才对:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title></title>  
    <script>
    //程序的作用是,先在页面上生成一个按键,这个按钮一按下去就再生成一个下拉框。 function writeDiv(context,divId)
     {
        var div = document.getElementById(divId);
        div.innerHTML = context; 
     }
    function test()
    {
      tmp="writeDiv(\"<select name='select23' id='select23' onchange='selectChange('select23');'><option value='1'>市场部<\/option><option value='2'>技术部<\/option><\/select>\",\"test\");";
      var s="<INPUT TYPE=\"button\" value=\"Create\" onclick=\""+tmp+";\">";
      writeDiv(s,"test");
    } </script>
    </head>
    <body>
    <div id="test">
    <script>
    test();
    </script>
      
    </body></html>
      

  3.   

    估计是变量tmp中的字符有问题。
      

  4.   

    <html>
    <head>
    <title></title>  
    <script>
    //程序的作用是,先在页面上生成一个按键,这个按钮一按下去就再生成一个下拉框。 function writeDiv(context,divId)
     {
        var div = document.getElementById(divId);
        div.innerHTML = context; 
     }
    function test()
    {
      var tmp="writeDiv('<select name=\\\'select23\\\' id=\\\'select23\\\' onchange=\\\'selectChange(&quot;select23&quot;);\\\'><option value=\\\'1\\\'>市场部<\/option><option value=\\\'2\\\'>技术部<\/option><\/select>','test');";
      var s="<INPUT TYPE=\"button\" value=\"Create\" onclick=\""+tmp+";\">";
      writeDiv(s,"test");
    } </script>
    </head>
    <body>
    <div id="test">
    <script>
    test();
    </script>
      <div onclick="alert( document.body.outerHTML )">test2</div>
    </body></html>
      

  5.   

    楼主,你这么写感觉结构颇不好,为什么不用createElement去创建呢?
      

  6.   

    我又将贴子加了二十分,有人能把我的例子改成用createElement去实现吗?
      

  7.   

    <html>
    <script>
    function Load()
    {
    var the = document.createElement( "INPUT" ) ;
    the.type = "button" ;
    the.value = "Create" ;
    the.onclick = function ()
    {
    var theSelect = document.createElement( "SELECT" ) ;
    theSelect.name = "select23" ;
    theSelect.id = "select23" ;
    //theSelect.onchange = new Function( "selectChange( 'select23' )" ) ;
    theSelect.onchange = new Function( "selectChange( this )" ) ;
    var theOp ;
    theOp = document.createElement("OPTION");
    theOp.value = "1" ;
    theOp.text = "市场部" ;
    theSelect.add( theOp ) ;
    theOp = document.createElement("OPTION");
    theOp.value = "2" ;
    theOp.text = "技术部" ;
    theSelect.add( theOp ) ;
    theSelect.selectedIndex = 0 ; writeDiv( theSelect, "theDiv" ) ;
    } writeDiv( the, "theDiv" ) ;}
    function writeDiv( the , theID )
    {
    var theObj = document.getElementById( theID );
    if( theObj == null ) 
    {
    return  ;
    }
    theObj.innerHTML = "" ;
    theObj.appendChild( the ) ;
    }
    function selectChange( theSelect )
    {
    var str = "下拉框状态:\n" ;
    str += "当前选中项的索引是   " + theSelect.selectedIndex  + "\n" ;
    str += "当前选中项的值是   " + theSelect.options[ theSelect.selectedIndex ].value + "\n" ;
    str += "当前选中的文字是   " + theSelect.options[ theSelect.selectedIndex ].text + "\n" ;
    alert( str )
    }
    </script>
    <body onload="Load()">
    <div id="theDiv"></div>
    </body>
    </html>