属性 + 的 CSS 规则 DOM 中的同级

发布于 2024-08-01 14:32:38 字数 2157 浏览 5 评论 0原文

我拥有的是一个表单,其中一个字段集包含多个其他字段集。 对于每个字段集,用户可以选中图例旁边的框并展开该字段集(至少在理论上)。 我本以为尝试使用纯 CSS 来实现这种效果会很酷,但我对如何根据 DOM 中两个元素彼此的位置编写规则感到困惑。 这是 HTML:

 <fieldset id="areasofinterest">
     <legend>Areas of Interest Survey</legend>

 <p>Please Check The Areas Applicable to you and Answer the Coorepsonding Questions.</p>

<!--Area 1 Checkbox and Questions -->
<fieldset class="area">
    <legend>
        <input type="checkbox" value="area1" id="area1" />
        <label for="area1"> Area 1 :</label>
    </legend>
    <ul>
        <li>
            <label for="area1_q1">Q1</label>
            <input type="text" name="area1_q1" id="area1_q1" />
        </li>
        <li>
            <label for="area1_q2">Q2</label>
            <input type="text" name="area1_q2" id="area1_q2" />
        </li>
    </ul>
</fieldset>

<!--Area 2 Checkbox and Questions -->
<fieldset class="area">
    <legend>
        <input type="checkbox" value="area2" id="area2" />
        <label for="other"> Area 2 :</label>
    </legend>

    <ul>
        <li>
            <label for="area2_q1">Q1</label>
            <input type="text" name="area2_q1" id="area2_q1" />
        </li>
        <li>
            <label for="area2_q2">Q2</label>
            <input type="text" name="area2_q2" id="area2_q2" />
        </li>
    </ul>
</fieldset>

  </fieldset>

起初,这个想法对我来说似乎有点 hack,但我通过 w3c 验证器运行它,并且 yipiee 它通过了嵌入在图例中的输入。 所以我确信我可以使用 javascript/jquery 得到我想要的东西,但我不想至少有一个使用 CSS 实现这一点的概念证明,即使旧的浏览器可能不支持它。

我尝试了这个规则:

.area ul {
    color: red;
 }

 .area input:checked + ul {
    color: blue;
 }

但没有运气。 我通过一个更简单的测试确认,如果只是一个输入后跟一个 ul,我正在使用的两种浏览器(Chrome、FF 3.5)都可以执行 input:checked + ul。 如果我有这两个规则(在简化版本中),它确实会切换颜色。

但我不确定是否有一种方法可以引用 UL,如果检查的元素位于图例标签内,因此不是 UL 的直接同级元素。 有没有办法说“包含已检查输入的图例的兄弟姐妹......”?

谢谢您的帮助。

What I have is a form with one fieldset containing several other fieldsets. For each fieldset, the user can check a box next to the legend and expand that fieldset (in theory, at least). I was thinking it would be cool to try using pure CSS for the effect, but I'm getting hung up on how to write the rule based on the two elements position to one another in the DOM. Here's the HTML:

 <fieldset id="areasofinterest">
     <legend>Areas of Interest Survey</legend>

 <p>Please Check The Areas Applicable to you and Answer the Coorepsonding Questions.</p>

<!--Area 1 Checkbox and Questions -->
<fieldset class="area">
    <legend>
        <input type="checkbox" value="area1" id="area1" />
        <label for="area1"> Area 1 :</label>
    </legend>
    <ul>
        <li>
            <label for="area1_q1">Q1</label>
            <input type="text" name="area1_q1" id="area1_q1" />
        </li>
        <li>
            <label for="area1_q2">Q2</label>
            <input type="text" name="area1_q2" id="area1_q2" />
        </li>
    </ul>
</fieldset>

<!--Area 2 Checkbox and Questions -->
<fieldset class="area">
    <legend>
        <input type="checkbox" value="area2" id="area2" />
        <label for="other"> Area 2 :</label>
    </legend>

    <ul>
        <li>
            <label for="area2_q1">Q1</label>
            <input type="text" name="area2_q1" id="area2_q1" />
        </li>
        <li>
            <label for="area2_q2">Q2</label>
            <input type="text" name="area2_q2" id="area2_q2" />
        </li>
    </ul>
</fieldset>

  </fieldset>

At first the idea seemed a bit hack-ish to me, but I ran it through the w3c validator, and yipiee it passed with inputs embedded within legends. So I'm sure I can get what I'm after using javascript/jquery, but I'd hate to at least have a proof of concept for doing it with CSS, even if older browsers may not support it.

I tried this rule:

.area ul {
    color: red;
 }

 .area input:checked + ul {
    color: blue;
 }

But with no luck. I confirmed with a much simpler test that both browsers I'm working with (Chrome, FF 3.5) can do input:checked + ul if it's simply an input followed by a ul. And it does toggle colors if I have both rules (in the simplified version).

But I'm not sure if there is a way to refer to the UL if the checked element is whithin the legend tag and thus not a direct sibling of the UL. Is there maybe a way to say "siblings of legends which contain inputs which are checked..."?

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┈┾☆殇 2024-08-08 14:32:38

没有。

“传说中的兄弟姐妹,其中包含
检查的输入...”

您的问题出在“包含”步骤。您不能像那样向上移动 DOM。不能根据子元素(或子元素的内容或属性)在 CSS 中选择元素。

您还使用表单元素进行视觉交互(和图例),这很奇怪,我认为您最好使用 javascript 我可以想象其他仅 CSS 的解决方案,但我认为没有一个与浏览器足够兼容。信任隐藏和显示形式。

Nope.

"siblings of legends which contain
inputs which are checked..."

Your problem is at the "contain" step. You can't move up the DOM like that. An element can't be selected in CSS based on children (or the children's contents or attributes).

You are also using a form element for visual interaction (and a legend), which is odd. I think you are better off with javascript. I can imagine other CSS-only solutions, but none that I would expect to be browser-compatible enough to trust with hiding and showing forms.

世态炎凉 2024-08-08 14:32:38

相邻选择器(+)显然选择相邻元素。 您需要一个父选择器,不幸的是它不存在(尽管过去向 w3c 提出了一些建议)。

Javascript(parentNode)是唯一的选择。

The adjacent selector (+) obviously selects adjacent elements. You need a parent selector, which unfortunately doesn't exist (despite some proposals made to the w3c in the past).

Javascript (parentNode) is the only choice.

温柔戏命师 2024-08-08 14:32:38

我的第一个想法是,这不能单独使用 CSS。 我认为如果没有 javascript,你将无法做到这一点。 例如,您可以使用 jQuery 中的选择器并通过 Javascript 获取父节点。

另外,我想知道在“检查”复选框后,CSS 模式是否会匹配 input[checked='checked'] ...例如:

.area input + ul{
// 未展开
}

.area 输入[checked='checked'] + ul{
// 展开

它不起作用,所以你需要使用 javascript 来实现选中/未选中的更改。 我也会使用之前建议的复选框以外的其他东西。

My first thought, is that this won't work with CSS alone. I don't think you'll be able to do this without javascript. You can use the selectors in jQuery, for example and get the parent node through Javascript.

Also, I wondered if, after 'checking' a checkbox, a CSS pattern would match input[checked='checked'] ... like:

.area input + ul{
// not expanded
}

.area input[checked='checked'] + ul{
// expanded
}

It didn't work though so you'd need to use javascript just to implement the change in checked / unchecked. I'd uses something other than a checkbox as suggested previously too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文