活动子项父项的复杂CSS选择器 [英] Complex CSS selector for parent of active child

查看:109
本文介绍了活动子项父项的复杂CSS选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法根据类中的子元素的类来选择父元素?与我相关的示例,该示例与 http://drupal.org 的精美菜单插件的HTML输出相关。输出呈现如下:

 < ul class =menu> 
< li>
< a class =active>活动页< / a>
< / li>
< li>
< a>一些其他页面< / a>
< / li>
< / ul>

我的问题是,是否可以对包含锚点的列表项应用样式用活动类就可以了。显然,我更喜欢列表项标记为活动,但我不能控制生成的代码。我可以使用javascript来执行这种事情(JQuery不介意),但我想知道是否有一个方法使用CSS选择器。



只是为了

解决方案

不幸的是,没有办法做到这一点


$ b // JavaScript代码:
document.getElementsByClassName(active)[0] .parentNode;

// jQuery代码:
$('。active')。parent()。get(0); //这将是< a>的父级< li> ;.


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>  

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.

解决方案

Unfortunately, there's no way to do that with CSS.

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选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆