JS动态为UL创建1个LI 为LI绑定一个Onlick事件。怎么做?
时间:2011-11-23
来源:互联网
作者: Unknownzh 发布时间: 2011-11-23
<!doctype html> <html> <head> <meta charset="gb2312" /> <title></title> <style> </style> </head> <body> <ul id="test"> </ul> <script> function $(o){return document.getElementById(o)} var li = document.createElement('li'); li.innerHTML = 123; li.onclick = function(){ alert(123) } $('test').insertBefore(li, $('test').firstChild) </script> </body> </html>
作者: ootwo 发布时间: 2011-11-23
用appendChild
作者: ifandui 发布时间: 2011-11-23
注意insertBefore的兼容性
用appendChild
up~`用 $('test').appendChild(li)
作者: MuBeiBei 发布时间: 2011-11-23
我测试了的,ie ff下都没问题。
作者: ootwo 发布时间: 2011-11-23
该回复于2011-11-23 10:47:07被管理员删除
- 对我有用[0]
- 丢个板砖[0]
- 引用
- 举报
- 管理
- TOP
|
#6楼 得分:0回复于:2011-11-23 11:26:59
|
作者: ootwo 发布时间: 2011-11-23
请说说在什么情况下 不兼容了?
我测试了的,ie ff下都没问题。
有人知道吗?
作者: acyexin 发布时间: 2011-11-23
$(document).ready(function() {
$("li.xxx").each(function() {
if (!$.data(this, 'events') || $.data(this, 'events').mousedown === undefined) {
$(this).mousedown(function() {
//do something
});
}
});
作者: ootwo 发布时间: 2011-11-24
$("ul>li").bind({
click:function(){
if($(this).hasClass("red")){
$(this).removeClass("red").addClass("blue");
}else{
$(this).removeClass("blue").addClass("red");
}
}
});
});
动态给li绑定click事件,点击li进行class的red,blue变换,参考下吧
作者: wdhans 发布时间: 2011-11-24
作者: printf_2012 发布时间: 2011-11-24
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28