AppendChild()不是一个函数javascript [英] AppendChild() is not a function javascript

查看:2722
本文介绍了AppendChild()不是一个函数javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javascript

this is my javascript

<head runat="server">
    <script type="text/javascript" language="javascript">

        function createQuestionPanel() {

            var element = document.createElement("Input");
            element.setAttribute("type", "button");
            element.setAttribute("value", "button");
            element.setAttribute("name", "button");


            var div = '<div>top div</div>';
            div[0].appendChild(element);

        }

        function formvalidate() {

        }

    </script>
</head>
<body onload="createQuestionPanel()">
</body>
</html>

抛出错误AppendChild不是函数。我试图寻找解决方案..有人建议,在

lockquote

it is throwing error "AppendChild is not a function" . I tried to search for solution .. it was suggested that on the place of

div.appendChild(element);

div.appendChild(element);

这应该被张贴


div [0] .appendChild (元素);

it didnt change the error . Please suggest

它没有改变错误。请提出建议

解决方案

推荐答案

var div = '<div>top div</div>';

字符串没有 appendChild 方法。而不是创建一个原始的HTML字符串,创建div作为一个DOM元素并附加一个文本节点,然后添加input元素:

Strings don't have an appendChild method. Instead of creating a raw HTML string, create the div as a DOM element and append a text node, then append the input element:

var div = document.createElement('div');
div.appendChild(document.createTextNode('top div'));

div.appendChild(element);

这篇关于AppendChild()不是一个函数javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆