简体   繁体   English

如何避免使用CSS属性选择器的JavaScript

[英]How to avoid using JavaScript with CSS attribute selector

I am trying to hide subsequent tr's with role="metadata" and the same data-group-id as the first occurring tr. 我试图用role =“metadata”和与第一个发生的tr相同的data-group-id隐藏后续的tr。

I cannot use JavaScript here and I am trying to achieve this using pure CSS. 我不能在这里使用JavaScript,我试图使用纯CSS实现这一点。

<table>
    <tbody>
        <!-- BEGIN this tr should be visible -->
        <tr data-group-id="1" role="metadata">
            <td>
                First rows group title
            </td>
        </tr>
        <!-- END this tr should be visible -->
        <tr data-group-id="1" role="data">
            <td>
                Row belonging to group 1
            </td>
        </tr>
        <!-- BEGIN this tr should be hidden -->
        <tr data-group-id="1" role="metadata">
            <td>
                Rows group title
            </td>
        </tr>
        <!-- END this tr should be hidden -->
        <tr data-group-id="1" role="data">
            <td>
                Another row belonging to group 1
            </td>
        </tr>
        <!-- BEGIN this tr should be visible -->
        <tr data-group-id="2" role="metadata">
            <td>
                Second rows group title
            </td>
        </tr>
        <!-- END this tr should be visible -->
        <tr data-group-id="2" role="data">
            <td>
                Row belonging to group 2
            </td>
        </tr>
    </tbody>
</table>

Selectors like this... 这样的选择器......

[data-group-id="1"][role~="metadata"] ~ [data-group-id="1"][role~="metadata"]
    display: none

... work very well, except that data-group-id may change dynamically. ...工作得很好,除了data-group-id可能会动态变化。

Something like this would be perfect (I know that this is invalid CSS code, its just my fantasy with regular expressions to help illustrating the problem): 像这样的东西是完美的(我知道这是无效的CSS代码,它只是我的幻想与正则表达式,以帮助说明问题):

[data-group-id="(.*?)"][role~="metadata"] ~ [data-group-id="\\1"][role~="metadata"]

Is there any way I can achieve this using only CSS? 有没有什么办法可以用CSS来实现这个目的?

Thanks in advance. 提前致谢。

Seems to me that using the data-group-id in CSS is impractical, especially since it's dynamically mutable and conditions of wether an element is hidden or not change. 在我看来,在CSS中使用data-group-id是不切实际的,特别是因为它是动态可变的,并且隐藏或不改变元素的条件。 You end up with a huge chunk of CSS thats impossible to maintain. 你最终得到了一大堆无法维护的CSS。

In the initial rendering, it might be better to add a className so you determine serverside wether the initial state should be shown or not. 在初始渲染中,最好添加一个className以便确定应该显示初始状态的服务器端。

<tr data-group-id="1" role="data" class="hidden">
    <td>Another row belonging to group 1</td>
</tr>

I am assuming JavaScript is used to dynamically change data-group-id , so why not use JavaScript to add/remove the className "hidden" when/where it makes sense. 我假设JavaScript用于动态更改data-group-id ,所以为什么不在有意义的时候使用JavaScript来添加/删除className “hidden”。 At least in JavaScript you CAN use regular expressions ;) 至少在JavaScript中你可以使用正则表达式;)

When you get to the point where you have to write impossible, long winded, error prone and unmaintainable CSS expressions, you're doing something wrong. 当你到达必须编写不可能的,冗长的,容易出错且不可维护的CSS表达式时,你做错了什么。

You're going to have to write some code to achieve this anyways, might as well do it the clean way instead of trying to shoehorn it into a styling language that isn't fit for the job. 无论如何,你将不得不编写一些代码来实现这一点,不妨尝试将它变成一种不适合工作的样式语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM