2021-01-08

CSS高级选择器详解:层次、属性及结构伪类
本文深入解析CSS中的高级选择器,包括层次选择器(后代、子、相邻兄弟和通用兄弟)、属性选择器(通过属性匹配元素)以及结构伪类选择器(如:first-child、:last-child等)。通过示例代码,详细阐述了这些选择器的工作原理和应用场景,帮助开发者更好地控制页面样式。

css高级选择器

1、层次选择器

层次选择器分为 :

  1. 后代选择器 用空格来表示   
  2. 子选择器 用  > 表示
  3. 相邻兄弟选择器 用 + 表示
  4. 通用兄弟选择器 用 ~ 表示

示例代码如下:

<style type="text/css">

		p,ul{
			border: 1px solid red;  /*边框属性*/
		}

		/*后代选择器*/
		/* body p{
			background: red;
		} */
		/*子选择器*/
		/* body>p{
			background: pink;
		} */
		/*/!*相邻兄弟选择器*!/*/
		/* .active + p{
			background: green;
		} */
		/*/!*通用选择器*!/*/
		 .active~p{
			background: yellow;
		} 
</style>
<body>
	<p  class="active">1</p>
	<p>2</p>
	<p>3</p>
	<ul>
		<li>
			<p>4</p>
		</li>
		<li>
			<p>5</p>
		</li>
		<li>
			<p>6</p>
		</li>
	</ul>
</body>

 

2、属性选择器

属性选择器采用的是中括号 [  ]  的形式对页面中的元素进行选择 

分为以下几种情况:

其中E表示元素名   attr表示属性名  val表示值

  • E[att]              匹配所有具有att属性的E元素,不考虑它的值

  • E[att=val]       匹配所有att属性等于"val"的E元素

  • E[att *=val]     匹配所有att属性中的值中包含 "val"  的E元素

  • E[att ^=val]     匹配所有att属性中的值中以 "val" 开头的E元素
  • E[att $=val]     匹配所有att属性中的值中以 "val" 结尾的E元素
<style type="text/css">
        /*此段代码只是让结构更美观,后续会详细讲解*/
        .demo a {
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            text-align: center;
            background: #aac;
            color: blue;
            font: bold 20px/50px Arial;
            margin-right: 5px;
            text-decoration: none;
            margin: 5px;
        }
        /*存在属性id的元素*/
        /* a[id] {
            background: yellow;
        } */
        /*/!*属性id=first的元素*!/*/
        /* a[id=first] {
            background: red;
        } */
        /*/!*属性class="links"的元素*!/*/
        /* a[class="links"] {
            background: red;
        } */
        /*/!*属性class里包含links字符串的元素*!/*/
        /* a[class*=links] {
            background: red;
        } */
        /*/!*属性href里以http开头的元素*!/*/
        /* a[href^=http] {
            background: red;
        } */
        /*/!*属性href里以png结尾的元素*!/*/
        a[href$=png] {
            background: red;
        }
    </style>
<body>
<p class="demo">
    <a href="http://www.baidu.com" class="links item first" id="first" >1</a>
    <a href="" class="links active item" title="test website" target="_blank" >2</a>
    <a href="sites/file/test.html" class="links item"  >3</a>
    <a href="sites/file/test.png" class="links item" > 4</a>
    <a href="sites/file/image.jpg" class="links item" >5</a>
    <a href="efc" class="links item" title="website link" >6</a>
    <a href="/a.pdf" class="links item" >7</a>
    <a href="/abc.pdf" class="links item" >8</a>
    <a href="abcdef.doc" class="links item" >9</a>
    <a href="abd.doc" class="linksitem last" id="last">10</a>
</p>
</body>

3、结构伪类选择器

选择器功能描述版本
E:last-child选择父元素的倒数第一个子元素E,相当于E:nth-last-child(1)3
E:nth-child(n)选择父元素的第n个子元素,n从1开始计算3
E:nth-last-child(n)选择父元素的倒数第n个子元素,n从1开始计算3
E:first-of-type选择父元素下同种标签的第一个元素,相当于E:nth-of-type(1)3
E:last-of-type选择父元素下同种标签的倒数第一个元素,相当于E:nth-last-of-type(1)3
E:nth-of-type(n)与:nth-child(n)作用类似,用作选择使用同种标签的第n个元素3
E:nth-last-of-type与:nth-last-child作用类似,用作选择同种标签的倒数第一个元素3

 

<style>
		li:first-of-type{
			background: red;
		}
		li:last-of-type{
			background: blue;
		}
		/* li:last-child{
			background: green;
		} */
		li:nth-child(3){
			background: yellow
		}
		li:nth-child(3){
			background: yellow
		}
	</style>
<body>
	<ul>
		<li>我是第1个li元素</li>
		<li>我是第2个li元素</li>
		<li>我是第3个li元素</li>
		<li>我是第4个li元素</li>
		<li>我是第5个li元素</li>
		<li>我是第6个li元素</li>
	</ul>
</body>

 

你已经成功将 `Date` 列转换为 `datetime64[ns]` 类型,并且输出如下: ``` 0 2021-01-04 1 2021-01-05 2 2021-01-06 3 2021-01-07 4 2021-01-08 Name: Date, dtype: datetime64[ns] ``` ✅ **这说明日期解析完全正确!** 接下来你应该执行: ```python df.set_index('Date', inplace=True) df.sort_index(inplace=True) # 确保时间顺序正确 ``` 然后进入主逻辑,使用我们之前修复过的区间筛选函数(作用于索引)即可正常运行。 --- ### ✅ 当前状态确认清单 | 检查项 | 是否完成 | 说明 | |--------|----------|------| | ✔️ `Date` 列是否为 `datetime64[ns]`? | ✅ 是 | 输出已验证 | | ✔️ 是否设置为索引? | ⚠️ 需手动执行 | 必须调用 `set_index` | | ✔️ 时间是否升序排列? | ⚠️ 建议排序 | 使用 `sort_index()` 更安全 | | ✔️ 区间过滤函数是否作用于 `index`? | ✅ 是 | 否则会出错 | --- ### ✅ 推荐后续代码(确保流程完整) ```python # 设置索引并排序 df.set_index('Date', inplace=True) df.sort_index(inplace=True) # 查看数据类型和前几行 print("Index dtype:", df.index.dtype) print("Data head:") print(df.head()) ``` 此时你的 `df.index` 是一个有序的 `DatetimeIndex`,可以安全用于以下操作: ```python intervals = { 'YTD': lambda idx: idx >= pd.Timestamp(year=idx[-1].year, month=1, day=1), '1Y': lambda idx: idx >= idx[-1] - pd.DateOffset(years=1), '6M': lambda idx: idx >= idx[-1] - pd.DateOffset(months=6), '3M': lambda idx: idx >= idx[-1] - pd.DateOffset(months=3), '1M': lambda idx: idx >= idx[-1] - pd.DateOffset(months=1), } ``` 因为 `idx[-1]` 就是最后一个交易日,比如 `2024-03-28`,它是一个 `pd.Timestamp`,支持 `.year`、`.month` 等属性,不会再报错! --- ### ✅ 示例:查看当前 YTD 起始日 你可以加一段调试代码来验证逻辑是否正确: ```python # 调试:打印各个区间的起始时间(以最后一个日期为基准) last_date = df.index[-1] print(f"最新交易日: {last_date}") print("各区间起始日:") print("YTD:", pd.Timestamp(year=last_date.year, month=1, day=1)) print("1Y: ", last_date - pd.DateOffset(years=1)) print("6M: ", last_date - pd.DateOffset(months=6)) print("3M: ", last_date - pd.DateOffset(months=3)) print("1M: ", last_date - pd.DateOffset(months=1)) ``` 输出示例: ``` 最新交易日: 2024-03-28 各区间起始日: YTD: 2024-01-01 1Y: 2023-03-28 6M: 2023-09-28 3M: 2024-01-28 1M: 2024-02-28 ``` 这些日期就是每个区间的“开始日”,之后用它们做布尔索引就能提取对应时间段的数据。 --- ### ✅ 总结:你现在可以继续了! 只要你完成了以下几步: 1. ✅ 成功将 `Date` 转为 `datetime64[ns]` 2. ✅ 执行了 `df.set_index('Date')` 和 `sort_index()` 3. ✅ 使用基于 `idx`(即 DatetimeIndex)的过滤函数 那么之前的两个错误(`float64 has no attribute year` 和 `>= not supported`)都已彻底解决,现在可以放心运行完整的绩效分析代码。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值