通过js新建个按钮,并且还要让这个新生成的按钮有onclick事件,然后当点击这个按钮时,让这个按钮的onclick事件调用带参数的js函数aaa(sUrl)
源代码如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">   
<title>无标题页</title>
<script type="text/javascript">
    function createButton()
    {
var nod=document.getElementById("div1");//这个div1是页面上的层
nod.innerHTML="<input id='Button100' type='button' value='让文本框显示' onclick='aaa(cccccccc)' />";
    }
function aaa(sUrl)
{
var text1=document.getElementById("Text1");//Text1是页面上的文本框
text1.value=sUrl;
}
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <input id="Button1" type="button" value="新建按钮" onclick="createButton()" />
    <div id="div1" style="border:solid 1px #336699;"></div>
    <input id="Text1" type="text" />
    </form>
</body>
</html>运行后,点击"新建按钮"按钮是生成了,可是执行新生成的按钮的onclick='aaa(cccccccc)'方法,无法让文本框Text1显示出cccccccc,查看js错误,提示说“'cccccccc'未定义”,怎样才能通过新生成的按钮把cccccccc传递给aaa(sUrl)函数,并且正常执行呢?本人js不好,但是判断是cccccccc没有正确的传入到aaa(sUrl)方法中!希望高手帮忙!

解决方案 »

  1.   


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server"> 
    <title>无标题页 </title>
    <script type="text/javascript">
    var cccccccc="cccccccc";
        function createButton()
        {
    var nod=document.getElementById("div1");//这个div1是页面上的层
    nod.innerHTML=" <input id='Button100' type='button' value='让文本框显示' onclick='aaa(cccccccc)' />";
        }
    function aaa(sUrl)
    {
    var text1=document.getElementById("Text1");//Text1是页面上的文本框
    text1.value=sUrl;
    }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <input id="Button1" type="button" value="新建按钮" onclick="createButton()" />
        <div id="div1" style="border:solid 1px #336699;"> </div>
        <input id="Text1" type="text" />
        </form>
    </body>
    </html>
      

  2.   


    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server"> 
    <title>无标题页 </title>
    <script type="text/javascript">
    var cccccccc="cccccccc";
        function createButton()
        {
    var nod=document.getElementById("div1");//这个div1是页面上的层
    nod.innerHTML=" <input id='Button100' type='button' value='让文本框显示' onclick='aaa(cccccccc)' />";
        }
    function aaa(sUrl)
    {
    var text1=document.getElementById("Text1");//Text1是页面上的文本框
    text1.value=sUrl;
    }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <input id="Button1" type="button" value="新建按钮" onclick="createButton()" />
        <div id="div1" style="border:solid 1px #336699;"> </div>
        <input id="Text1" type="text" />
        </form>
    </body>
    </html>
      

  3.   

    你在JS第一句定义一下cccccccc就可以了.
      

  4.   

    ccccccc是什么东西?
    你这么写的话,js会把cccccccc当成是一个变量,所以说它未定义,你试试这么改呢:
    nod.innerHTML=" <input id='Button100' type='button' value='让文本框显示' onclick='aaa(\"cccccccc\")' />"; 
      

  5.   

    OK了!谢谢2楼和6楼和7楼的!尤其6楼的,就是不知道怎么让js理解cccccccc是字符串而不是对象的问题!结贴!