javascript中createElement的两种创建方式
作者:永远爱好写程序 时间:2024-04-10 16:13:42
本文实例讲述了javascript中createElement的两种创建方式。分享给大家供大家参考。具体实现方法如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CreateElement的两种创建方式</title>
<script type="text/javascript">
function CreateButton1() {
var btn = document.createElement("input");
btn.type = "button";
btn.value = "我是动态创建的1";
btn.onclick = function () {
alert(this.value);
}
document.body.appendChild(btn);
}
function CreateButton2() {
var btn = document.createElement("<input type='button' value='我是动态创建的2' "+"onclick='OnClick2(this)' />");
document.body.appendChild(btn);
}
function OnClick2(btn) {
alert(btn.value);
}
function CreateLink() {
var link = document.createElement("<a href='http://www.baidu.com'>百度</a>");
//注意这里链接的文本“百度”是不会显示出来的,
//必须设置innerText或innerHTML
link.innerText = "百度";
document.body.appendChild(link);
}
function CreateLabel() {
var lbl = document.createElement("label");
lbl.setAttribute("for", "userName");
lbl.setAttribute("myAge", "12");
//可以设置自定义标示
lbl.innerText = "用户名:";
document.body.appendChild(lbl);
}
</script>
</head>
<body>
<input type="button" value="动态创建按钮1" onclick="CreateButton1()" />
<input type="button" value="动态创建按钮2" onclick="CreateButton2()" />
<input type="button" value="动态创建链接" onclick="CreateLink()" />
<input type="button" value="动态创建Label" onclick="CreateLabel()" />
<input type="text" id="userName" value="李莫" />
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
标签:javascript,createElement
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简单介绍Python中的RSS处理
2023-03-03 13:59:17
Python入门及进阶笔记 Python 内置函数小结
2021-09-30 10:15:52
CSS背景图片的运用优化HTTP连接数
2008-09-04 21:38:00
![](https://img.aspxhome.com/file/UploadPic/20089/4/200894214417934s.gif)
Sql Server 2008完全卸载方法(其他版本类似)第1/2页
2024-01-14 01:20:09
![](https://img.aspxhome.com/file/2023/1/129321_0s.jpg)
python 检测nginx服务邮件报警的脚本
2023-08-04 17:34:10
超详细注释之OpenCV构建透明的叠加层
2021-08-18 14:51:01
![](https://img.aspxhome.com/file/2023/1/134431_0s.gif)
asp 通用修改和增加函数代码
2011-03-16 11:15:00
python matplotlib绘制三维图的示例
2023-03-04 16:35:48
![](https://img.aspxhome.com/file/2023/6/121646_0s.png)
js实现简单选项卡功能
2024-04-22 13:05:47
![](https://img.aspxhome.com/file/2023/3/135803_0s.png)
图片完美缩放
2024-04-22 13:07:22
Vue.js 利用v-for中的index值实现隔行变色
2024-04-16 09:35:40
浅析php中array_map和array_walk的使用对比
2023-09-10 22:22:28
asp如何设置cookie的过期时间
2008-02-29 13:36:00
Python计算已经过去多少个周末的方法
2023-03-10 11:40:52
探讨:Oracle数据库查看一个进程是如何执行相关的实际SQL语句
2024-01-16 19:07:50
python之随机数函数的实现示例
2023-07-17 04:44:41
![](https://img.aspxhome.com/file/2023/0/98980_0s.png)
4个场景教会你Go中Goroutine和通道是怎么用的
2024-04-23 09:45:05
Python 中的Selenium异常处理实例代码
2021-03-11 12:42:11
![](https://img.aspxhome.com/file/2023/1/110201_0s.png)
教你利用Python破解ZIP或RAR文件密码
2022-04-25 07:11:15
python 实现图片裁剪小工具
2022-04-27 18:15:15
![](https://img.aspxhome.com/file/2023/9/104999_0s.png)