Js和Jquery动态添加删除类
来源:互联网 发布:mac 删除office 编辑:程序博客网 时间:2024/06/05 04:56
Js和Jquery动态添加删除节点和类
导语:
本节介绍Js和Jquery动态添加删除节点、类的方法。
js方法:createElement、appendChild、removeChild、setAttribute、getAttribute
var overlay = document.createElement("div");//创建节点overlay.setAttribute("id","overlay");//添加id属性overlay.setAttribute("class","overlay");//添加class属性img.src = this.getAttribute("src");//获取src属性内容document.body.appendChild(overlay);//添加节点document.body.removeChild(document.getElementById("img"));//移除节点
jquery方法:removeClass、addClass、attr
$("#overlay").removeClass("displaynone");//移除类$("#overlay").addClass("overlay");//添加类$("#overlayimage").attr('src',this.getAttribute("src"));//添加属性
demo完整代码Jquery:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"/><title>点击图片显示大图无图片Jquery</title><script src="js/jquery-3.2.1.min.js"></script><style> img{padding:5px;width:100px;height:auto;} #outer{ width:100%; height:100%; } .overlay{ display: block; background-color:#000; opacity: .7; filter:alpha(opacity=70); position: fixed; top:0; left:0; width:100%; height:100%; z-index: 10; } .overlayimg{ position: absolute; z-index: 11; left:50px; top:30px; width:auto; } .displaynone{ display:none; }</style><script> function expandPhoto(){ $("#overlay").removeClass("displaynone"); $("#overlay").addClass("overlay"); $("#overlayimage").attr('src',this.getAttribute("src")); $("#overlayimage").onclick=restore(); } function restore(){ $("#overlay").addClass("displaynone"); $("#overlay").removeClass("overlay"); } window.onload = function(){ var imgs = document.getElementsByTagName("img"); imgs[0].focus(); for(var i = 0;i<imgs.length;i++){ imgs[i].onclick = expandPhoto; imgs[i].onkeydown = expandPhoto; } }</script></head> <body><div id = "outer"><p>点击图片</p><img src="http://e.hiphotos.baidu.com/image/pic/item/77094b36acaf2edde7684cc38e1001e93901937a.jpg" /><img src="http://e.hiphotos.baidu.com/image/pic/item/77094b36acaf2edde7684cc38e1001e93901937a.jpg" /></div><div id="overlay" class="displaynone"><img class="overlayimg" id="overlayimage"/></div></body></html>
demo完整代码Js:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"/><title>点击图片显示大图无插件JS</title><style> img{padding:5px;width:100px;height:auto;} #outer{ width:100%; height:100%; } .overlay{ background-color:#000; opacity: .7; filter:alpha(opacity=70); position: fixed; top:0; left:0; width:100%; height:100%; z-index: 10; } .overlayimg{ position: absolute; z-index: 11; left:50px; top:30px; width:auto; }</style><script> function expandPhoto(){ var overlay = document.createElement("div"); overlay.setAttribute("id","overlay"); overlay.setAttribute("class","overlay"); document.body.appendChild(overlay); var img = document.createElement("img"); img.setAttribute("class","overlayimg"); img.src = this.getAttribute("src"); document.getElementById("overlay").appendChild(img); img.onclick = restore; } function restore(){ document.body.removeChild(document.getElementById("overlay")); document.body.removeChild(document.getElementById("img")); } window.onload = function(){ var imgs = document.getElementsByTagName("img"); imgs[0].focus(); for(var i = 0;i<imgs.length;i++){ imgs[i].onclick = expandPhoto; imgs[i].onkeydown = expandPhoto; } }</script></head><body><div id = "outer"><p>点击图片</p><img src="http://e.hiphotos.baidu.com/image/pic/item/77094b36acaf2edde7684cc38e1001e93901937a.jpg" /><img src="http://e.hiphotos.baidu.com/image/pic/item/77094b36acaf2edde7684cc38e1001e93901937a.jpg" /></div></body></html>
阅读全文
0 0
- Js和Jquery动态添加删除类
- jquery实现动态添加和删除表格
- jquery动态添加和删除class属性
- jQuery中动态添加和删除行
- js动态添加和删除一行
- JS动态添加option和删除option
- JS动态添加option和删除option
- JS动态添加option和删除option
- JS动态添加option和删除option
- JS实现动态添加和删除DIV
- js动态添加表格,全选和删除
- JS动态添加option和删除option
- JS动态添加option和删除option
- 原生态JS和JQuery版的动态添加、删除表格行
- 原生JS和JQuery动态添加、删除表格行的方法
- jquery及js动态添加删除option示例
- js动态添加标签和动态删除标签
- JQuery动态添加删除表格
- Sangmado 公共基础类库
- 原生AJAX
- UVA1628 送匹萨 [有关未来费用的区间dp (伪)升级版]
- windows上php7环境搭建
- 行走方案问题(动态规划实现)
- Js和Jquery动态添加删除类
- Hexo自定义页面的方法
- css笔记——文本样式
- android版本
- android 7.0调用相机闪退问题
- 4.1.5ReentrantLock与Condition选择性的唤醒线程
- bzoj3018 [Usaco2012 Nov]Distant Pastures 最短路
- follow集 first集 LL(1)文法判别
- 查看python 里已经安装的第三方库的版本号