简体   繁体   English

通用同级组合器 (~),不更新 DOM 更改,按预期工作?

[英]General sibling combinator (~), not updating on DOM changes, working as intended?

I had a go at at this question (this question isn't related at all to his question), and tried to solve it through applying CSS selectors depending on whether the checkboxes had been ticked.我在这个问题上有一个 go (这个问题与他的问题完全无关),并试图通过应用 CSS 选择器来解决它,具体取决于复选框是否已被勾选。 The idea I had, was that if there is an element which is :checked , the preceeding submit button should be visible.我的想法是,如果有一个元素是:checked ,那么前面的提交按钮应该是可见的。 The resulting CSS I came up with was:我想出的结果 CSS 是:

input[type=checkbox]:checked ~ input[type=submit] {
    display:block;
}

Now, when the page is loaded, the submit button is hidden, as it should.现在,当页面被加载时,提交按钮被隐藏了,这是它应该的。 But, when you check a box, nothing happens (presumably it should?).但是,当您选中一个框时,什么也没有发生(大概应该?)。

If you load the form with one input field with attribute checked, it does have the submit button visible, as it should, but when you untick it, the submit button doesn't hide.如果您加载带有选中属性的输入字段的表单,它确实有可见的提交按钮,因为它应该,但是当您取消它时,提交按钮不会隐藏。

When you clone the the form (with one checkbox ticked) through Javascript (and the whole purpose of this exercise was not to use Javascript), it does do the expected result, ie either hides the submit button if no checkboxes were checked, or displays it if a checkbox was ticked.当您通过 Javascript 克隆表单(勾选一个复选框)时(并且本练习的全部目的不是使用 Javascript),它确实会产生预期的结果,即如果未选中任何复选框,则隐藏提交按钮,或显示如果勾选了复选框。

In other words, that css rule does not take into account changes to the dom, but does take it into account when new elements are put in. Is that working as intended?换句话说,css 规则没有考虑到对 dom 的更改,但在放入新元素时会考虑到它。这是否按预期工作? First I've seen something like that happen with CSS.首先,我看到 CSS 发生了类似的事情。

Example: http://jsfiddle.net/niklasvh/nn4Qw/示例: http://jsfiddle.net/niklasvh/nn4Qw/

Did I miss something here or isn't it supposed to work as I've described?我在这里错过了什么,或者它不应该像我描述的那样工作吗?

Edit tested with firefox, and works fine, so this seems to be an issue with google chrome.编辑用 firefox 测试,工作正常,所以这似乎是谷歌浏览器的问题。 Not tried with later versions of IE or Opera.未尝试使用更高版本的 IE 或 Opera。

Your CSS looks correct, but browser support, of course, varies, and where there is support, there will be bugs.您的 CSS 看起来是正确的,但是浏览器支持当然会有所不同,支持的地方就会有错误。 Javascript would be much more reliable. Javascript 会更可靠。

Webkit had problems with siblings and andjacents when combined with pseudoclases.当与伪类结合使用时,Webkit 会出现兄弟姐妹和邻接的问题。 I had some problems with:hover, it's possible that the same thing happens with:checked.我遇到了一些问题:hover,可能会发生同样的事情:checked。 It should be fixed in recent nightly chromium builds.它应该在最近的夜间铬版本中得到修复。 If so this bug will some time end in stable chrome version.如果是这样,这个错误将在一段时间内以稳定的 chrome 版本结束。

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

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