简体   繁体   English

我可以将 CSS 样式应用于元素名称吗?

[英]Can I apply a CSS style to an element name?

I'm currently working on a project where I have no control over the HTML that I am applying CSS styles to.我目前正在开发一个项目,在该项目中我无法控制应用 CSS 样式的 HTML。 And the HTML is not very well labelled, in the sense that there are not enough id and class declarations to differentiate between elements.并且 HTML 没有很好地标记,因为没有足够的 id 和 class 声明来区分元素。

So, I'm looking for ways I can apply styles to objects that don't have an id or class attribute.因此,我正在寻找可以将样式应用于没有 id 或 class 属性的对象的方法。

Sometimes, form items have a "name" or "value" attribute:有时,表单项具有“名称”或“值”属性:

<input type="submit" value="Go" name="goButton">

Is there a way I can apply a style based on name="goButton"?有没有办法可以应用基于 name="goButton" 的样式? What about "value"? “价值”呢?

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.这是一种很难找到的东西,因为 Google 搜索会找到各种情况,其中“名称”和“价值”等宽泛的术语会出现在网页中。

I'm kind of suspecting the answer is no... but perhaps someone has a clever hack?我有点怀疑答案是否定的......但也许有人有一个聪明的黑客?

Any advice would be greatly appreciated.任何建议将不胜感激。

You can use the attribute selector,您可以使用属性选择器,

 input[name="goButton"] { background: red; }
 <input name="goButton">

Be aware that it isn't supported in IE6.请注意,它在 IE6 中不受支持。

Update: In 2016 you can pretty much use them as you want, since IE6 is dead.更新:在 2016 年,您几乎可以随心所欲地使用它们,因为 IE6 已死。 http://quirksmode.org/css/selectors/ http://quirksmode.org/css/selectors/

http://reference.sitepoint.com/css/attributeselector http://reference.sitepoint.com/css/attributeselector

Text Input Example文本输入示例

 input[type=text] { width: 150px; } input[name=myname] { width: 100px; }
 <input type="text"> <br> <input type="text" name="myname">

You can use attribute selectors but they won't work in IE6 like meder said, there are javascript workarounds to that tho.您可以使用属性选择器,但它们不能像 meder 所说的那样在 IE6 中工作,有 javascript 解决方法。 Check Selectivizr检查选择性

More detailed into on attribute selectors: http://www.css3.info/preview/attribute-selectors/更详细地了解属性选择器: http : //www.css3.info/preview/attribute-selectors/

/* turns all input fields that have a name that starts with "go" red */
input[name^="go"] { color: red }

For future googlers, FYI, the method in the answer by @meder , can be used with any element that has a name attribute, so lets say theres an <iframe> with the name xyz then you can use the rule as belows.对于未来的谷歌人,仅供参考,@meder 的答案中的方法可以与任何具有 name 属性的元素一起使用,所以假设有一个名为xyz<iframe>然后你可以使用如下规则。

iframe[name=xyz] {    
    display: none;
}   

The name attribute can be used on the following elements: name属性可用于以下元素:

  • <button>
  • <fieldset>
  • <form>
  • <iframe>
  • <input>
  • <keygen>
  • <map>
  • <meta>
  • <object>
  • <output>
  • <param>
  • <select>
  • <textarea>

Using [name=elementName]{} without tag before will work too.使用之前没有标签的[name=elementName]{}也可以。 It will affect all elements with this name.它将影响具有此名称的所有元素。

For example:例如:

 [name=test] { width: 100px; }
 <input type=text name=test> <div name=test></div>

If i understand your question right then,如果我当时明白你的问题,

Yes you can set style of individual element if its id or name is available,是的,如果其 id 或名称可用,您可以设置单个元素的样式,

eg例如

if id available then u can get control over the element like,如果 id 可用,那么你可以控制元素,例如,

<input type="submit" value="Go" name="goButton">

var v_obj = document.getElementsById('goButton');

v_obj.setAttribute('style','color:red;background:none');

else if name is available then u can get control over the element like,否则,如果名称可用,则您可以控制元素,例如,

<input type="submit" value="Go" name="goButton">

var v_obj = document.getElementsByName('goButton');

v_obj.setAttribute('style','color:red;background:none');

This is the perfect job for the query selector...这是查询选择器的完美工作......

var Set1=document.querySelectorAll('input[type=button]');  // by type

var Set2=document.querySelectorAll('input[name=goButton]'); // by name

var Set3=document.querySelectorAll('input[value=Go]'); // by value

You can then loop through these collections to operate on elements found.然后,您可以遍历这些集合以对找到的元素进行操作。

if in case you are not using name in input but other element, then you can target other element with there attribute.如果您不在输入中使用名称而是在其他元素中使用名称,那么您可以使用 there 属性定位其他元素。

 [title~=flower] { border: 5px solid yellow; }
 <img src="klematis.jpg" title="klematis flower" width="150" height="113"> <img src="img_flwr.gif" title="flower" width="224" height="162"> <img src="img_flwr.gif" title="flowers" width="224" height="162">

hope its help.希望它的帮助。 Thank you谢谢

 input[type=text] { width: 150px; length: 150px; } input[name=myname] { width: 100px; length: 150px; }
 <input type="text"> <br> <input type="text" name="myname">

have you explored the possibility of using jQuery?您是否探索过使用 jQuery 的可能性? It has a very reach selector model (similar in syntax to CSS) and even if your elements don't have IDs, you should be able to select them using parent --> child --> grandchild relationship.它有一个非常广泛的选择器模型(在语法上类似于 CSS),即使您的元素没有 ID,您也应该能够使用父 --> 子 --> 孙子关系来选择它们。 Once you have them selected, there's a very simple method call (I forget the exact name) that allows you to apply CSS style to the element(s).一旦你选择了它们,就会有一个非常简单的方法调用(我忘记了确切的名称),它允许你将 CSS 样式应用于元素。

It should be simple to use and as a bonus, you'll most likely be very cross-platform compatible.它应该易于使用,并且作为奖励,您很可能非常具有跨平台兼容性。

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

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