<div class="main"> <div class="title"> xxx </div> <div class="A">我想要改变这里的样式(即首个class="A"的div)</div> <div class="A"></div> </div>
不用jquery,能不能做到?
回复讨论(解决方案)
你再给他设置一个class,就像这样
<div class="main"> <div class="title"> xxx </div> <div class="A div1">我想要改变这里的样式(即首个class="A"的div)</div> <div class="A"></div></div>
然后这样子写
.A.div1{color:red;}
.main .A:first-child{
background-color:yellow;
}
搞定!!
楼上正解:
:first-child 选择器用于选取属于其父元素的首个子元素的指定选择器。
正解个毛线,请问你们有测过吗,你都说了:first-child 是取其父元素的首个子元素,你确定他那样子可以?你确定他找得到?
@风中的少年 ,你确定你写对了?@转角遇到我得爱
.main:first-child .A{background-color:yellow;}这样子才对
也不对,我还是坚持我最初的方法
function getByClass(cls){ if(!document.getElementsByClassName){ return document.getElementsByClassName(cls); } else { var all = document.getElementsByTagName('*'), reg = new RegExp('(^|\\s)' + cls + '(\\s|$)'), arr = []; for (var i = 0; i < all.length; i++) { if(reg.test(all[i].className)){ arr.push(all[i]); } }; return arr; }}getByClass('A')[0].style.color = 'red';
正解个毛线,请问你们有测过吗,你都说了:first-child 是取其父元素的首个子元素,你确定他那样子可以?你确定他找得到?
@风中的少年 ,你确定你写对了?@转角遇到我得爱
哥们你能不能去测测呀
所以你改变一下方式就好了,让其成为父类的首个子元素。变通。。。。。。。
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Jslet - Template</title> <script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script> <style> .test .A:first-child{ background-color:yellow; } </style> </head><body><div class="main"> <div class="title"> xxx </div> <div class="test"> <div class="A">我想要改变这里的样式(即首个class="A"的div)</div> <div class="A">sad</div> </div></div></body></html>
可以曲线救国:
<!doctype html><html><head><meta charset="UTF-8"><title>选择器</title><style>.main .A {color:#F00;}.main .A ~ .A {color:#000;}</style></head><body><div class="main"> <div class="title"> xxx </div> <div class="A">我想要改变这里的样式(即首个class="A"的div)</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div></div></body></html>
:first-child
:nth-child(1)
:first-of-type
:nth-of-type(1)
都是行不通的,
10楼写得很麻烦, 和下面效果差不多
div.A:nth-of-type(1)
但都是行不通的
<!doctype html><html><head><meta charset="UTF-8"><title>选择器</title><style>.main .A {color:#F00;}.main .A ~ .A {color:#000;}div.A:nth-of-type(1){background: green;}</style></head><body><div class="main"> <div class="title"> xxx </div> <div class="A">我想要改变这里的样式(即首个class="A"的div)</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div></div><div class="main"> <div class="A">否定10楼</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div> <div class="A">222222222</div></div><div> <div class="A">否定:nth-of-type(1)</div></div></body></html>
:first-child
:nth-child(1)
:first-of-type
:nth-of-type(1)
都是行不通的,
10楼写得很麻烦, 和下面效果差不多
div.A:nth-of-type(1)
但都是行不通的
[/code]
你用的是什么浏览器?
http://test.ddcat.net/test/brotherSelector.html
你用的是什么浏览器?
http://test.ddcat.net/test/brotherSelector.html
对楼主要求的理解是整个页面只能让唯一第一个 class="A" 的 div 的样式生效, 任何其他地方也生效则不符要求.
我没有找到仅用 CSS 达到要求的办法
你的方式均会让 "否定10楼" 也变成红色, 测试浏览器: chrome 44.0, firefox 39.0.3, safari 5.1.7, ie8
样式优先级问题?
1、首先优先级
内联样式表 > 内部样式表 > 外部样式表 > 浏览器缺省样式表
2、 在优先级相同时 考虑css权重值
大概就是 行内 > id > class 比较多 可以百度下慢慢研究
3、再然后就是考虑远近了
浏览器从左向右 从上到下的执行一个网页 后面的会覆盖前面的
分给我把
妈呀 有必要考虑那么深么 用内联可好
都是神啊,给第一个一个id不就可以了!!!!
都是神啊,给第一个一个id不就可以了!!!!
他就不给弄个 id, 难道去咬么??
无语
如果整个页面仅有这个一个class=A需要处理,给其一个唯一ID或 特殊 class 即可
-----------------------------------------------------
如果非要折腾,使用css选择:
如果你的 首个 class=A 始终处于 .main 的 第二个元素( 或已知固定位置),且浏览器支持 nth-child,这个问题相对简单:
关键就是 首个 class=A 的位置是已知的,改变 nth-child(2) 即可;
1、所有.main 的第二个元素(即首个 class=A)改变样式
.main>:nth-child(2){color:#fff; background:#B32B2D;}
2、仅页面第一个.main 的第二个元素(即首个 class=A)改变样式
.main:nth-child(1)>:nth-child(2){color:#fff; background:#B32B2D;}
妈呀 有必要考虑那么深么 用内联可好
都是神啊,给第一个一个id不就可以了!!!!
居然忘了来看了,主要这个

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.