小白教程
所有教程
关于
Search
172.70.126.199
172.70.126.199
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Met document createelement”的源代码
本页内容
上一节:
Met_document_createdocumentfragment
下一节:
Met_document_createtextnode
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:document.createElement()}}[[Category:dom obj document|12]] = HTML DOM createElement() 方法 = [[dom obj document|Document 对象]] 创建一个按钮: <sample title="" desc="" lang="html" hererun="1"> var btn=document.createElement("BUTTON"); </sample> 输出结果: <sample title="" desc="" lang="html" hererun="1"> </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p id="demo">单击按钮创建button元素</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var btn=document.createElement("BUTTON"); document.body.appendChild(btn); }; </script> </body> </html> </run> HTML元素经常包含文本。创建指定文本的按钮你需要在按钮元素后添加文本节点: 创建指定文本的按钮: <sample title="" desc="" lang="html" hererun="1"> var btn=document.createElement("BUTTON"); var t=document.createTextNode("CLICK ME"); btn.appendChild(t); </sample> 输出结果: <sample title="" desc="" lang="html" hererun="1"> CLICK ME </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p id="demo">单击按钮创建有文本的按钮</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var btn=document.createElement("BUTTON"); var t=document.createTextNode("CLICK ME"); btn.appendChild(t); document.body.appendChild(btn); }; </script> </body> </html> </run> == 定义和用法 == createElement() 方法通过指定名称创建一个元素 == 浏览器支持 == 所有主要浏览器都支持 createElement() 方法 == 语法 == <pre> document.createElement(nodename)</pre> == 参数 == {| class="table table-striped table-hover" ! 参数 ! 类型 ! 描述 |- | nodename | String | 必须。创建元素的名称。 |} == 返回值 == {| class="table table-striped table-hover" ! 类型 ! 描述 |- | 元素对象 | 创建的元素节点 |} == 技术细节 == {| class="table table-striped table-hover" | DOM 版本 | Core Level 1 Document Object |} [[dom obj document|Document 对象]]
返回至“
Met document createelement
”。
上一节:
Met_document_createdocumentfragment
下一节:
Met_document_createtextnode