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的页面底部固 ...
随机推荐
- C#获取根目录的方法总结
1.控制台应用程序 static void Main(string[] args) { //1.Environment.CurrentDirectory Console.WriteLine(Envir ...
- springMVC中 @RequestParam和@RequestBody的区别
首先,不可以同时传进@RequestParam和@RequestBody,好像可以传进两个@RequestParam 如果不加@requestparam修饰,相当于 加上@requestparam且各 ...
- 查看oracle当前的连接数
SQL> select count(*) from v$session #当前的连接数SQL> Select count(*) from v$session where status='A ...
- Ch02 课堂作业
测试一:运行结果: 测试二:运行结果: 测试三:运行结果:
- 使用工具intellij idea 进行java web开发简介
一.工具下载及安装准备 1.首先下载工具 Intellij idea ,下载地址:https://www.jetbrains.com/ 2.破解 百度下载一个 JetbrainsCrack-2.7- ...
- [Kubernetes]谈谈Kubernetes的本质
当下k8s算是比较火的一个内容,那么它到底是什么呢,它为什么会这么火呢,它解决的是什么问题呢.这篇文章就尝试着来讲讲,Kubernetes的本质. 当我们谈Kubernetes的时候,总是会想起来Do ...
- 华为mate10 UA
Dalvik/2.1.0 (Linux; U; Android 9; ALP-AL00 Build/HUAWEIALP-AL00) "user-agent": "Mozi ...
- shim和polyfill
shim是将新的api引入旧的环境 polyfill是一段代码或插件 https://www.aliyun.com/jiaocheng/773254.html 理解得不够透彻...
- iOS rebuild from bitcode对ipa大小的影响
https://developer.apple.com/library/content/technotes/tn2432/_index.html 为了测试一下rebuild from bitcode的 ...
- nginx 配置身份验证 http_auth_basic_module
ngx_http_auth_basic_module模块实现访问必须输入用户名和密码 正确情况向访问,这为我们一些重要资源访问增添了一道安全锁. 语法: auth_basic_user_fil ...