博主信息
博文 12
粉丝 1
评论 1
访问量 14476
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
第一天学习JQuery
Lucifer的博客
原创
1174人浏览过
<!DOCTYPE html>    
<html lang="en">    
<head>    
<meta charset="UTF-8">    
<title>原生querySelector()与querySelectorAll()获取元素实例</title>    
<!--引入jquery静态库,用cdn方式-->    
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>    
<style>    
ul{padding: 0;margin: 0;}    
li{list-style: none;padding-left: 20px;}    
/*弹窗*/    
.mei { position: fixed; width: 500px; height: 500px; z-index: 100000; left: 50%; top: 50%; margin: -100px 0 0 -150px; background: #fff; border: 1px solid #ddd; border-radius: 6px; box-shadow: 1px 1px 2px #aaa; }    
.mei .c { position: absolute; width: 30px; height: 30px; right: 0; top: 0; font-size: 20px; font-style: normal; text-align: center; cursor: pointer; z-index: 2; }    
.mei .img { padding: 10px; text-align: center; border-bottom: 1px solid #ddd; border-top: 1px solid #ddd; }    
.mei .img img { width: 400px; height: 400px; }    
</style>    
<script>    
$(document).ready(function () {    
//    原生queryselector()添加样式    
document.querySelector('.main').setAttribute("style","width: 800px;height: 800px; margin: auto; box-shadow: 2px 2px 2px 5px #888;overflow:hidden;")    
document.querySelector('.list').setAttribute("style","width: 98%; background-color: #000;margin:10px auto")    
//原生queryselectorall()获取所有a元素    
var a = document.querySelectorAll('a')    
//    2.循环a元素并去掉a默认样式    
for (var i=0; i<a.length ;i++){    
//两种写法经测试都可以使用    
//        a[i].style.cssText="text-decoration: none;line-height: 40px;color: #fff;"    
//        a[i].setAttribute("style","text-decoration: none;line-height: 40px;color: #fff;")    
//用jQuery方法写    
$('a').css({"line-height": "40px", "text-decoration": "none","color": "#fff",})    
}    
//鼠标经过list块变色    
$('.list').on('mouseenter',function () {    
$(this).css('background','#888')    
})    
//鼠标移除变回原来样式    
$('.list').on('mouseleave',function () {    
$(this).css('background','red')    
})    
//            鼠标点击第一条信息显示一张图片    
$(".wechat").click(function () {    
var url = $(this).attr("data-img")    
//alert(url)    
img = "<div class='mei'>"    
img += "<i class='c'>x</i>"    
img += "<div class='img'><img src='" + url + "'></div>"    
img += "</div>"    
$("body").append(img)    
})    
//关闭弹出图片    
$(document).on("click", ".mei .c", function () {    
//alert(0)    
$(".mei").remove();    
})    
//点击第二条移除第一条的美女弹出    
$(".wechat2").click(function () {    
$(".wechat").off("click");    
})    
})    
</script>    
</head>    
<body>    
<div class="main">    
<div class="list">    
<ul>    
<li><a href="javascript:void(0)" class="wechat" data-img="https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1513663374&di=326ae2a1e7012f5e7af05047cc7928e4&src=http://img3.duitang.com/uploads/item/201605/27/20160527205144_8Kh3w.jpeg">这是个美女,不信点下看!</a></li>    
<li><a href="javascript:void(0)" class="wechat2">点击这一条,上面的弹出美女就不会弹出了!</a></li>    
<li><a href="javascript:void(0)">使用PHP快速构建命令行应用程序的方法</a></li>    
<li><a href="javascript:void(0)">JavaScript 中数组操作注意点基础</a></li>    
<li><a href="javascript:void(0)">HTML5中 Canvas 的 3D 压力器反序列化</a></li>    
<li><a href="javascript:void(0)">three.js实现炫酷的3d影院实例分享</a></li>    
</ul>    
</div>    
</div>    
</body>    
</html>


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
1条评论
Lucifer 2017-12-19 14:39:34
写的有点乱,主要是多练习下JS控制CSS样式!
1楼
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学