pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

第 n 個嵌套元素的選擇器

selector for nth nested elements(第 n 個嵌套元素的選擇器)
本文介紹了第 n 個嵌套元素的選擇器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在研究無法確定可嵌套性的樹形視圖,但想定義一些嵌套規則以進行樣式設置.例如,我希望第一級項目具有特定的邊框.緊挨在下方的嵌套項目具有不同的邊框.我有一個工作示例,但它是靜態且冗長的.我知道必須有更好的方法使用選擇器,但我似乎無法讓它發揮作用.這是我目前的解決方案-

I am working on a tree view of undeterminable nestability, but would like to define some nested rules for styling. For example, I want the first level item to have a particular border. Nested items immediately underneath to have a different border. I have a working example, but it is static and verbose. I know there has to be a better way using selectors, but I can't seem to make it work. Here is my current solution-

.item {
    border-left-color: #somecolor1;
}
.item > .item {
    border-left-color: #somecolor2;
}
.item > .item > .item {
    border-left-color: #somecolor3;
}
.item > .item > .item > .item {
    border-left-color: #somecolor4;
}
.item > .item > .item > .item > .item {
    border-left-color: #somecolor5;
}

所以這行得通,但顯然它有點冗長.有沒有更好的辦法?

So this works, but obviously it is kind of verbose. Is there a better way?

推薦答案

在 CSS 中,選擇器字符串主要描述嵌套結構,目前不存在任何分代跳過選擇器,因此理論上您可能會執行類似 的操作.item:nth-grandchild(4) 替換您的第五個示例.

In CSS the selector string is largely describing the nesting structure, and there does not currently exist any generational skipping selectors such that you might theoretically do something like .item:nth-grandchild(4) to replace your fifth example.

如果減少 css 的冗長對您來說非常重要(假設您有多達 10 甚至 100 級的嵌套正在打開),那么您真的需要考慮修改 html 本身以減少需要CSS.這可以通過服務器端腳本(PHP 等)或客戶端腳本(Javascript)動態完成,或者由您靜態完成.您選擇哪種方式取決于多種因素.

If reducing verbosity of your css is of high importance to you (lets say you have up 10 or even 100 levels of nesting you are switching on), then really you need to look into modifying the html itself in order to reduce the css needed. That can be done dynamically via server-side scripting (PHP, etc.), or client-side scripting (Javascript), or statically by you. Which way you choose will depend on a variety of factors.

html修改可以是更具體的類或者直接樣式屬性的形式,但我推薦前者.這里至少有四種減少 css 的方法:

The html modification can be in the form of more specific classes or direct style properties, but I recommend the former. Here are at least four ways css would be reduced:

#1 多個類,一個指示級別

示例 HTML

<div class="item L-1">
 <div class="item L-2">
  <div class="item L-3">
  </div>
 </div>
</div>

示例 CSS

.item.L-1 {
    border-left-color: #somecolor1;
}
.item.L-2 {
    border-left-color: #somecolor2;
}
.item.L-3 {
    border-left-color: #somecolor3;
}

#2 多個類,一種指示顏色

示例 HTML

<div class="item LBC-1"> 
 <div class="item LBC-2">
  <div class="item LBC-3">
  </div>
 </div>
</div>

示例 CSS

.item.LBC-1 {
    border-left-color: #somecolor1;
}
.item.LBC-2 {
    border-left-color: #somecolor2;
}
.item.LBC-3 {
    border-left-color: #somecolor3;
}

#3 單個類名表示級別

示例 HTML

<div class="item-L1"> 
 <div class="item-L2">
  <div class="item-L3">
  </div>
 </div>
</div>

示例 CSS

[class *= "item-"] {
    /* common css properties for the items goes here */
}

.item-L1 {
    border-left-color: #somecolor1;
}
.item-L2 {
    border-left-color: #somecolor2;
}
.item-L3 {
    border-left-color: #somecolor3;
}

#4 每個項目的樣式屬性

示例 HTML

<div class="item" style="border-left-color: #somecolor1"> 
 <div class="item" style="border-left-color: #somecolor2">
  <div class="item"  style="border-left-color: #somecolor3">
  </div>
 </div>
</div>

示例 CSS

/* none to control color */

最佳"的討論

通常動態解決方案最終會生成類似于 #4 的 html,這最終會使 html 變得非常冗長,我個人不建議這樣做.但是,那些動態解決方案不需要這樣做,而是可以添加類名,如 #1-3.

Discussion of "Best"

Often dynamic solutions end up producing html like that of #4, which ends up making the html very verbose, and I personally would not recommend it. However, those dynamic solutions do not need to do that, but could instead add class names like #1-3.

最終什么是最好的"在很大程度上取決于您想要實現的目標、您擁有多少控制權以及其他哪些屬性也需要更改.就個人而言,我也會避免使用 #2,因為它通過將類名與左邊框顏色"關聯起來,開始將演示文稿與 html 聯系在一起.對我來說,解決方案 #1 或 #3 是最好的,因為它們只是設置類來幫助 css 了解 .item 處于什么級別",然后允許針對該級別進行特定定位任何你可能需要它的水平.

What is ultimately "best" depends a lot on what you are trying to achieve, how much control you have, and what other properties need changing as well. Personally, I would avoid #2 as well, because it begins to tie presentation too much to html by having a class name associated with the "left border color." To me, solution #1 or #3 would be best, as those are simply setting classes that help the css to know what "level" the .item is at, which then allows for specific targeting to that level for anything you may need it for.

當然,如果您真的要處理 100 個嵌套級別,那么即使對于解決方案 #1-3,您也可能需要研究一些 css 預處理器來生成所需的 100 個級別的代碼.但是 css 輸出仍然遠遠少于使用當前方法所需的長選擇器字符串.

Of course, if you were really dealing with 100 nested levels, then even for solutions #1-3, you might want to look into some css preprocessor to generate the 100 levels of code needed. But the css output would still be far less than the long selector strings needed using the current method you are doing.

這篇關于第 n 個嵌套元素的選擇器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Style every third element?(每隔三個元素設置樣式?)
Why shouldn#39;t I use ID selectors in CSS?(為什么我不應該在 CSS 中使用 ID 選擇器?)
What does img[class*=quot;alignquot;] mean in CSS?(CSS 中的 img[class*=“align] 是什么意思?)
CSS: Last element on line(CSS:最后一個元素)
How do I select every other div class element using just CSS (no js)(如何僅使用 CSS(無 js)選擇所有其他 div 類元素)
Tool for checking unused CSS selectors?(檢查未使用的 CSS 選擇器的工具?)
主站蜘蛛池模板: 喷砂机厂家_自动除锈抛丸机价格-成都泰盛吉自动化喷砂设备 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 温室大棚建设|水肥一体化|物联网系统| 电车线(用于供电给电车的输电线路)-百科 | 卧涛科技有限公司科技项目申报公司|高新技术企业申报|专利申请 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 闪电优家-卫生间防水补漏_酒店漏水渗水维修_防水堵漏公司 | 丹尼克尔拧紧枪_自动送钉机_智能电批_柔性振动盘_螺丝供料器品牌 | 专业甜品培训学校_广东糖水培训_奶茶培训_特色小吃培训_广州烘趣甜品培训机构 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 有声小说,听书,听小说资源库-听世界网 | 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | sfp光模块,高速万兆光模块工厂-性价比更高的光纤模块制造商-武汉恒泰通 | 无缝方管|无缝矩形管|无缝方矩管|无锡方管厂家 | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 首页 - 军军小站|张军博客 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 洗石机-移动滚筒式,振动,螺旋,洗矿机-青州冠诚重工机械有限公司 | 心得体会网_心得体会格式范文模板 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 并网柜,汇流箱,电控设备,中高低压开关柜,电气电力成套设备,PLC控制设备订制厂家,江苏昌伟业新能源科技有限公司 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 环氧树脂地坪_防静电地坪漆_环氧地坪漆涂料厂家-地壹涂料地坪漆 环球电气之家-中国专业电气电子产品行业服务网站! | 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 | 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 北京开源多邦科技发展有限公司官网| 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | PE拉伸缠绕膜,拉伸缠绕膜厂家,纳米缠绕膜-山东凯祥包装 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 老城街小面官网_正宗重庆小面加盟技术培训_特色面馆加盟|牛肉拉面|招商加盟代理费用多少钱 | 天津仓库出租网-天津电商仓库-天津云仓一件代发-【博程云仓】 | 云南成考网_云南成人高考报名网| 包塑软管|金属软管|包塑金属软管-闵彬管业| 致胜管家软件服务【在线免费体验】| 广州昊至泉水上乐园设备有限公司|