Can I apply a CSS style to an element name?

后端 未结 10 1300
借酒劲吻你
借酒劲吻你 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:41

    have you explored the possibility of using 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. 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).

    It should be simple to use and as a bonus, you'll most likely be very cross-platform compatible.

    0 讨论(0)
  • 2020-12-07 14:42

    Using [name=elementName]{} without tag before will work too. It will affect all elements with this name.

    For example:

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

    0 讨论(0)
  • 2020-12-07 14:49

    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.

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

    The name attribute can be used on the following elements:

    • <button>
    • <fieldset>
    • <form>
    • <iframe>
    • <input>
    • <keygen>
    • <map>
    • <meta>
    • <object>
    • <output>
    • <param>
    • <select>
    • <textarea>
    0 讨论(0)
  • 2020-12-07 14:52

    If i understand your question right then,

    Yes you can set style of individual element if its id or name is available,

    e.g.

    if id available then u can get control over the element like,

    <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');
    
    0 讨论(0)
  • 2020-12-07 14:54

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

    0 讨论(0)
  • 2020-12-07 14:58

    if in case you are not using name in input but other element, then you can target other element with there attribute.

        [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

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