小码哥的IT人生

CSS [attribute] 选择器 详解

css3基础 2023-07-22 16:29:27小码哥的IT人生shichen

CSS [attribute] 选择器

实例

为带有 target 属性的 <a> 元素设置样式:

a[target]
{
background-color:yellow;
}

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
a[target]
{
background-color:yellow;
}
</style>
</head>
<body>
<p>带有 target 属性的链接会得到黄色背景:</p>
<a href="http://www.phpcodeweb.com">www.phpcodeweb.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>
<p><b>注释:</b>对于 IE8 及更早版本的浏览器中的 [<i>attribute</i>],必须声明 <!DOCTYPE>。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

浏览器支持

表格中的数字注明了完全支持该属性的首个浏览器版本。

选择器 Chrome IE Firefox Safari Opera
[attribute] 4.0 7.0 2.0 3.1 9.6

注释:对于 IE8 及更早版本的浏览器中的 [attribute],必须声明 <!DOCTYPE>

定义和用法

[attribute] 选择器用于选取带有指定属性的元素。

相关页面

CSS 教程:CSS 属性选择器

CSS 教程:CSS 属性选择器详解

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024