The CSS input[value=whatever] selector doesn't

2020-05-07 10:27发布

问题:

N.B.: I should note that the proper solution to this is to just use the 'placeholder' attribute of an input, but the question still stands.

Another N.B.: Since, as Quentin explains below, the "value" attribute stores the default value, and the input.value IDL attribute stores the current value, the JavaScript I used to "fix" the problem in my below example is non-conforming, as it uses the (non-IDL) value attribute to store current, rather than default, values. Besides, it involves DOM access on every key press, so it was always just a flawed demo of the problem I was having. It's actually quite terrible code and shouldn't be used ever.

CSS selectors made me think that I could make an input with a label that acts as a preview without any JS. I absolutely position the input at 0,0 inside the label (which is displayed as an inline-block) and give it a background of "none", but only if it's got a value of "" and isn't focussed, otherwise it has a background colour, which obscures the label text.

The HTML5 spec says that input.value reflects the current value of an input, but even though input.value updates as you type into an input, CSS using the input[value=somestring] selector applies based only on what was explicitly typed into the document, or set in the DOM by the JavaScript setAttribute method (and perhaps by other DOM-altering means).

I made a jsFiddle representing this.

Just in case that is down, here is an HTML document containing the relevant code:

<!doctype html>
    <head>
        <meta charset="utf-8" />
        <title>The CSS Attribute selector behaves all funny</title>

        <style>
            label {
                display: inline-block;
                height: 25px;
                line-height: 25px;
                position: relative;
                text-indent: 5px;
                min-width: 120px;
            }
            label input[value=""] {
                background: none;
            }
            label input, label input:focus {
                background: #fff;
                border: 1px solid #666;
                height: 23px;
                left: 0px;
                padding: 0px;
                position: absolute;
                text-indent: 5px;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <form method="post">
            <p><label>name <input required value=""></label></p>
        </form>
        <p><button id="js-fixThis">JS PLEASE MAKE IT BETTER</button></p>
        <script>
            var inputs = document.getElementsByTagName('input');
            var jsFixOn = false;
            for (i = 0; i < inputs.length; i++) {
                if (inputs[i].parentNode.tagName == 'LABEL') { //only inputs inside a label counts as preview inputs according to my CSS
                    var input = inputs[i];
                    inputs[i].onkeyup= function () {
                        if (jsFixOn) input.setAttribute('value', input.value);
                    };
                }
            }

            document.getElementById('js-fixThis').onclick = function () {
                if (jsFixOn) {
                    this.innerHTML = 'JS PLEASE MAKE IT BETTER';
                    jsFixOn = false;
                } else {
                    this.innerHTML = 'No, actually, break it again for a moment.';       
                    jsFixOn = true;
                }
            };
        </script>
    </body>
</html>

I could be missing something, but I don't know what.

回答1:

The value attribute sets the default value for the field.

The value property sets the current value for the field. Typing in the field also sets the current value.

Updating the current value does not change the value attribute.

Attribute selectors only match on attribute values.



回答2:

There are new pseudo classes for matching a number of properties of an input element

  • :valid
  • :invalid
  • :in-range
  • :out-of-range
  • :required

A required element with no value set to it will match against :invalid. If you insist on using the value instead of placeholder, you could simply add a pattern or a customValidity function to force your initial value to be counted as invalid.