Complex CSS selector for parent of active child(活动孩子的父母的复杂 CSS 选择器)
问题描述
有没有办法根据类中子元素的类来选择父元素?与我相关的示例与 http://drupal.org 的漂亮菜单插件的 HTML 输出有关.输出呈现如下:
Is there a way to select a parent element based on the class of a child element in the class? The example that is relevant to me relating to HTML output by a nice menu plugin for http://drupal.org. The output renders like this:
<ul class="menu">  
    <li>  
        <a class="active">Active Page</a>  
    </li>  
    <li>    
        <a>Some Other Page</a>  
    </li>  
</ul>  
我的问题是是否可以将样式应用于包含带有活动类的锚的列表项.显然,我更希望将列表项标记为活动,但我无法控制生成的代码.我可以使用 javascript(想到 JQuery)来执行此类操作,但我想知道是否有办法使用 CSS 选择器来执行此操作.
My question is whether or not it is possible to apply a style to the list item that contains the anchor with the active class on it. Obviously, I'd prefer that the list item be marked as active, but I don't have control of the code that gets produced. I could perform this sort of thing using javascript (JQuery springs to mind), but I was wondering if there is a way to do this using CSS selectors.
为了清楚起见,我想将样式应用于列表项,而不是锚点.
Just to be clear, I want to apply a style to the list item, not the anchor.
推荐答案
很遗憾,CSS 无法做到这一点.
Unfortunately, there's no way to do that with CSS.
不过,使用 JavaScript 并不难:
It's not very difficult with JavaScript though:
// JavaScript code:
document.getElementsByClassName("active")[0].parentNode;
// jQuery code:
$('.active').parent().get(0); // This would be the <a>'s parent <li>.
                        这篇关于活动孩子的父母的复杂 CSS 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:活动孩子的父母的复杂 CSS 选择器
				
        
 
            
        - addEventListener 在 IE 11 中不起作用 2022-01-01
 - Flexslider 箭头未正确显示 2022-01-01
 - 400或500级别的HTTP响应 2022-01-01
 - 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
 - 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
 - Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
 - CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
 - 失败的 Canvas 360 jquery 插件 2022-01-01
 - Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
 - Fetch API 如何获取响应体? 2022-01-01
 
						
						
						
						
						