我知道我可以根据元素的名称将 CSS 样式应用到该元素:
input[name=description] {
width: 200px;
}
Run Code Online (Sandbox Code Playgroud)
但是是否可以将 CSS 样式应用到名称中包含特定字符串的元素呢?
想象一下几个输入:
<input type="text" name="projectDescription"/>
<input type="text" name="themeDescription"/>
<input type="text" name="methodologyDescription"/>
<input type="text" name="somethingElse"/>
etc...
Run Code Online (Sandbox Code Playgroud)
因此,我只想将样式应用于名称包含单词“Description”的输入。这可以在不编写脚本的情况下完成吗?
是的你可以:
\n\ninput[name*="Description"] {\n width: 200px;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n[attr] \n Represents an element with an attribute name of attr.\n[attr=value]\n Represents an element with an attribute name of attr \n and whose value is exactly "value".\n[attr~=value]\n Represents an element with an attribute name of attr \n whose value is a whitespace-separated list of words, \n one of which is exactly "value".\n[attr|=value]\n Represents an element with an attribute name of attr. \n Its value can be exactly \xe2\x80\x9cvalue\xe2\x80\x9d or can begin with \xe2\x80\x9cvalue\xe2\x80\x9d\n immediately followed by \xe2\x80\x9c-\xe2\x80\x9d (U+002D). It can be used for \n language subcode matches.\n[attr^=value]\n Represents an element with an attribute name of attr \n and whose value is prefixed by "value".\n[attr$=value]\n Represents an element with an attribute name of attr \n and whose value is suffixed by "value".\n[attr*=value]\n Represents an element with an attribute name of attr \n and whose value contains at least one occurrence of \n string "value" as substring.\nRun Code Online (Sandbox Code Playgroud)\n\n\n