Can I apply a CSS style to an element name?

后端 未结 10 1352
借酒劲吻你
借酒劲吻你 2020-12-07 14:39

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 a

相关标签:
10条回答
  • 2020-12-07 14:59

    Text Input Example

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

    0 讨论(0)
  • 2020-12-07 15:02

    You can use the attribute selector,

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

    Be aware that it isn't supported in IE6.

    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

    0 讨论(0)
  • 2020-12-07 15:03

    You can use attribute selectors but they won't work in IE6 like meder said, there are javascript workarounds to that tho. Check Selectivizr

    More detailed into on 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 }
    
    0 讨论(0)
  • 2020-12-07 15:03

    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.

    0 讨论(0)
提交回复
热议问题