<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gbk" />
< title >例子:使用CSS3选择器,兼容ie6/7 ,firefox,opera,safari </title>
< style type ="text/css" >
/*div[id^="focus"]表示id属性值以focus-开头的那些div*/
div[id^="focus-"]{ background-color:#696;}
</style>
< !--[if IE 6] >
< script type ="text/javascript" src ="jquery.js" > </script>
< script type ="text/javascript" >
$(document).ready(function(){
$("div[id^='focus-']").css('background-color','#696');
});
</script>
< ![endif]-- >
</head>
< body >
< div id ="focus-div1" >这个会高亮背景 </div>
< div id ="div2" >普通div </div>
< div id ="div3" >普通div </div>
< div id ="div4" >普通div </div>
< div id ="focus-div5" >这个会高亮背景 </div>
</body>
</html>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gbk" />
< title >例子:使用CSS3选择器,兼容ie6/7 ,firefox,opera,safari </title>
< style type ="text/css" >
/*div[id^="focus"]表示id属性值以focus-开头的那些div*/
div[id^="focus-"]{ background-color:#696;}
</style>
< !--[if IE 6] >
< script type ="text/javascript" src ="jquery.js" > </script>
< script type ="text/javascript" >
$(document).ready(function(){
$("div[id^='focus-']").css('background-color','#696');
});
</script>
< ![endif]-- >
</head>
< body >
< div id ="focus-div1" >这个会高亮背景 </div>
< div id ="div2" >普通div </div>
< div id ="div3" >普通div </div>
< div id ="div4" >普通div </div>
< div id ="focus-div5" >这个会高亮背景 </div>
</body>
</html>
这里使用了ie条件注释单独对ie6做了处理。
注:大家可以实验下其他用法
div[name='hello']属性值等于某个值的情况;
div[id$='3']以某个字串结束的元素;
div[id*='div']包含字串的;
div[name]表示含有name属性的那些div;
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/143528
,如需转载请自行联系原作者