document.querySelectorAll() 兼容 IE6
不多说,直接上代码
// 使用 css 选择器获取元素对象 兼容性封装 Test Already.
function getElementsByCss(cssStr){
if(document.querySelectorAll){
return document.querySelectorAll(cssStr);
}else{
var style = document.createElement('style');
var elements = [];
var ele; document.documentElement.firstChild.appendChild(style);
document._qsa = []; style.styleSheet.cssText = cssStr + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
window.scrollBy(0, 0); // 滚动条滑到最上方
style.parentNode.removeChild(style); while (document._qsa.length) {
ele = document._qsa.shift();
ele.style.removeAttribute('x-qsa');
elements.push(ele);
}
document._qsa = null;
return elements;
}
}
实例代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title> <link rel="stylesheet" type="text/css" href="./css/index.css" /> <script type="text/javascript" src="./js/kjfFunctions.js"></script>
<script type="text/javascript" src="./js/index.js"></script> <style rel="stylesheet" type="text/css" >
#outer {
margin: 0 auto; background: #ccc;
} #middle_box {
float: left;
width: 800px;
height: 800px; background: #eee;
} #middle {
width: 200px;
height: 200px;
margin: 0 auto; background: yellowgreen;
} #middle .theTest1 {
width: 50px;
height: 50px;
margin: 0 auto; background: pink;
} #middle .theTest2 {
width: 50px;
height: 50px;
margin: 0 auto; background: deeppink;
} #left_box {
float: left;
width: 200px;
height: 200px; background: skyblue;
} #right_box {
float: left;
width: 200px;
height: 200px; background: yellow;
} </style>
</head> <body>
<div id="follow_mouse" class="unSelectedAble">
<img src="./img/xiao.gif" />
</div> <div id="outer" class="clearfix"> <div id="left_box"></div>
<div id="middle_box">
<div id="middle">
<div class="clearfix theTest1"></div>
<div class="clearfix theTest2"></div>
</div>
</div>
<div id="right_box"></div>
</div> <!-- javascript 代码 -->
<script type="text/javascript"> // 使用 css 选择器获取元素对象 兼容性封装
function getElementsByCss(cssStr){
if(document.querySelectorAll){
return document.querySelectorAll(cssStr);
}else{
var style = document.createElement('style');
var elements = [];
var ele; document.documentElement.firstChild.appendChild(style);
document._qsa = []; style.styleSheet.cssText = cssStr + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
window.scrollBy(0, 0); // 滚动条滑到最上方
style.parentNode.removeChild(style); while (document._qsa.length) {
ele = document._qsa.shift();
ele.style.removeAttribute('x-qsa');
elements.push(ele);
}
document._qsa = null;
return elements;
}
} var the1 = getElementsByCss("#middle .theTest1")[0];
var the2 = getElementsByCss("#middle .theTest2")[0]; the1.style.backgroundColor = "red";
the2.style.backgroundColor = "red";
</script>
</body>
</html>
document.querySelectorAll() 兼容 IE6的更多相关文章
- JavaScript简单分页,兼容IE6,~3KB
简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...
- sizzle分析记录:关于querySelectorAll兼容问题
querySelector和querySelectorAll是W3C提供的新的查询接口 目前几乎主流浏览器均支持了他们.包括 IE8(含) 以上版本. Firefox. Chrome.Safari.O ...
- html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器
以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上 ...
- 表单美化-原生javascript和jQuery多选按钮(兼容IE6)
前些天我们讲了下单选按钮的美化今天来做表单元素多选按钮的美化.我们的想法是:利用多选按钮是否被选中和是否不给选择的特性来为按钮的父元素添加对应的样式,就是说用什么的样式是由按钮的状态来决定. 用到的图 ...
- PNG兼容IE6解决方法
虽然说现在早就不用ie6浏览器了,可以还是有一小部分还在使用 ,刚好公司也有要求~~~ <p> E6不兼容png图片,确实让网页的图片质量大大下降,为了兼容万恶的IE6,总结了下面几种方法 ...
- html5标签兼容ie6,7,8
注册博客园已经三年了,这三年一直在忙,没时间写博文.也许是忙,也许是懒吧!当然这三年发生了很多事,我也从开发人员转变为前端人员. 是时候对所学的,所用的知识做一下沉淀了.就从这一篇开始吧! html5 ...
- 兼容IE6的页面底部固定层CSS代码
有时候当我们需要把一个元素固定在页面的某个部位,一般都是用css中的“position:fixed;”方法来解决,但是IE6不支持fixed,所以今天分享一个兼容IE6的页面底部固定层CSS代码.如下 ...
- 给Select赋值 innerHTML 不兼容IE6\IE7\IE8\IE9
<select class="b-select" id="location-province" name="Province" def ...
- 浮动层固定兼容IE6 position:fixed的最佳解决方案
第一种:css方法 有时候当我们需要把一个元素固定在页面的某个部位,一般都是用css中的“position:fixed;”方法来解决,但是IE6不支持fixed,所以今天分享一个兼容IE6的页面底部固 ...
随机推荐
- SpringBoot系列: Maven多项目管理
这篇是 maven 项目管理的第二篇, 讲解使用 maven 进行多个项目管理, 之前有一篇是 maven 的基础知识. SpringBoot系列: Eclipse+Maven环境准备 一个完整的解决 ...
- 学习python笔记 协程
下面将一个经典的消费者和生产者的案例进行分析: import time def consumer(): r = '' while True: n = yield r if not n: return ...
- 在Windows上安装Arduino-IDE
Arduino IDE的官方下载地址为:http://arduino.cc/en/Main/Software 也可以从我的网盘下载:win系统 1.8.9版本 链接:https://pan.baidu ...
- 【汇总目录】C#
[2019年04月29日] C# textbox 自动滚动 [2019年02月07日] C#利用VUDP.cs开发网络通讯应用程序 [2019年02月06日] C#利用VINI.cs操作INI文件 [ ...
- Nginx--服务部署、基于域名的虚拟主机配置
一.服务部署 1.预处理 安装CentOS ,配置hosts.静态IP.设置必要的安全参数等(略) 1-1.系统环境 [root@vnx ~]# cat /etc/redhat-release Cen ...
- Linux设置SSH登录(SecureCrt)
背景 每次登录需要输入复杂的密码,而且不停的有人在尝试登录root账户.感觉心慌慌,所以不得不设置更加安全的登录方式. 配置SSH无密码登录需要4步 准备工作 生成公钥和私钥 导入公钥到认证文件,更改 ...
- [Linux容器]当我们谈容器的时候,我们在谈什么
Docker在当下很火,那么,当我们谈Docker,谈容器的时候,我们在谈什么? 或者说,你对Docker,对容器了解吗?容器,到底是怎么一回事儿? 这篇文章着重来讲一下Linux容器,为什么强调Li ...
- C#+EntityFramework编程方式详细之Code First
Code First Code First模式即“代码优先”模式,是从EF4.1开始新建加入的功能.使用Code First模式进行EF开发时只需要编写对应的数据类,然后自动生成数据库. Code F ...
- 怎么给PDF文件交换页面
在使用PDF文件的时候有文件页面的排版错误的时候,这个时候就需要交换页面了,那么怎么给PDF文件交换页面呢,在使用PDF文件的时候需要交换页面的时候要怎么做呢,下面小编就为大家分享一下PDF文件交换页 ...
- IT行业中文资源网址集绵
1. IT网址:https://github.com/ityouknow/awesome-list 2.后端架构师网址:https://github.com/xingshaocheng/archite ...