問題描述
我目前正在從事一個項目,我無法控制我應用 CSS 樣式的 HTML.而且 HTML 沒有很好地標記,因為沒有足夠的 id 和 class 聲明來區分元素.
I'm currently working on a project where I have no control over the HTML that I am applying CSS styles to. And the HTML is not very well labelled, in the sense that there are not enough id and class declarations to differentiate between elements.
所以,我正在尋找可以將樣式應用于沒有 id 或 class 屬性的對象的方法.
So, I'm looking for ways I can apply styles to objects that don't have an id or class attribute.
有時,表單項具有名稱"或值"屬性:
Sometimes, form items have a "name" or "value" attribute:
<input type="submit" value="Go" name="goButton">
有沒有一種方法可以應用基于 name="goButton" 的樣式?價值"呢?
Is there a way I can apply a style based on name="goButton"? What about "value"?
這種情況很難找到,因為 Google 搜索會發現各種情況,其中名稱"和價值"等廣義術語會出現在網頁中.
It's the kind of thing that's hard to find out because a Google search will find all sorts of instances in which broad terms like "name" and "value" will appear in web pages.
我有點懷疑答案是否定的......但也許有人有一個聰明的黑客?
I'm kind of suspecting the answer is no... but perhaps someone has a clever hack?
任何建議將不勝感激.
推薦答案
可以使用屬性選擇器,
input[name="goButton"] {
background: red;
}
<input name="goButton">
請注意,IE6 不支持它.
Be aware that it isn't supported in IE6.
更新:在 2016 年,您幾乎可以隨意使用它們,因為 IE6 已死.http://quirksmode.org/css/selectors/
Update: In 2016 you can pretty much use them as you want, since IE6 is dead. http://quirksmode.org/css/selectors/
http://reference.sitepoint.com/css/attributeselector
這篇關于我可以將 CSS 樣式應用于元素名稱嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!