【问题标题】:Assign CSS attributes according to class "range"根据类“范围”分配CSS属性
【发布时间】:2012-11-10 08:52:57
【问题描述】:

我有一些 HTML 代码需要设置样式,这些代码由 Web 服务自动生成。我得到的部分代码如下所示:

<input type='text' id='txtField001' class='txtField150'>foo</input>    
<input type='text' id='txtField002' class='txtField10'>bar</input>
<input type='text' id='txtField001' class='txtField142'>sum</input>

现在,“奇怪”的部分来了:类名后面的数字(即:150、10 和 142)实际上是最大值。文本字段接受的字符数,它给了我一个关于文本字段应该呈现多宽的“提示”:150 宽,10 短;由于这个数字变化很大(它是由调用 Web 服务的用户定义的),让“n”个类符合所有可能的值是不切实际的。

那么:有没有办法拥有一个“范围类”或类似的东西 - 请记住,理论上,我无法更改 Web 服务提供的任何内容,并且我真的不想用javascript?

具体来说,有没有办法声明这样的事情(我知道这有点疯狂):

.txtField1 ... .txtField50 {
    width: 50px;
}

.txtField50 ... .txtField100 {
    width: 80px;
}

.txtField100 ... .txtField150 {
    width: 120px;
}

(我在脑海中是如何阅读这个的:“对于从 1 到 50 的任何类 txtField,使用 50px 宽度......等等)

感谢您的帮助。我知道这是一个很长的机会,我最好的选择是使用 javascript,但是,嘿,我不得不问 ;-)

【问题讨论】:

    标签: css class css-selectors


    【解决方案1】:

    是的,有限制

    我一直在思考一个类似于 cimmanon 的解决方案,只是我知道它需要比这更精致(这就是为什么它需要一些时间来回答)。

    让我先声明一下,这可能需要一个实际限制(我不知道在您的情况下是否有字符数限制)。 As you can see in my example fiddle,任何 300+ 都无法解析为更大的尺寸。如果存在上限或未知上限,那么 javascript 确实是您的最佳解决方案。我的示例适用于少于 300 个,并且可能使用不多的代码就可以制作多达 999 个。但是1000+我认为是不合理的。

    CSS

    /* set default as small size */
    [class ^= "txtField"] {
        width: 50px;
    }
    
    /* weed out 50-99, making sure not to get 5-9 */
    [class *= "d5"]:not([class $= "d5"]),
    [class *= "d6"]:not([class $= "d6"]),
    [class *= "d7"]:not([class $= "d7"]),
    [class *= "d8"]:not([class $= "d8"]),
    [class *= "d9"]:not([class $= "d9"])
    {
        width: 80px;
    }
    
    /* weed out 100-199, making sure not to get 1 or 10-19
       NOTE: this becomes a highly specific selector
    */
    [class *= "d1"]:not([class $= "d1"]):not([class $= "d10"]):not([class $= "d11"]):not([class $= "d12"]):not([class $= "d13"]):not([class $= "d14"]):not([class $= "d15"]):not([class $= "d16"]):not([class $= "d17"]):not([class $= "d18"]):not([class $= "d19"])
    {
        width: 120px;
    }
    
    /* weed out 150-199, making sure not to get 15-19
       NOTE: because the previous selector is so specific, this one
       needed the !important flag (which I hate to use, but here
       seemed to be the best and only solution)
    */
    [class *= "d15"]:not([class $= "d15"]),
    [class *= "d16"]:not([class $= "d16"]),
    [class *= "d17"]:not([class $= "d17"]),
    [class *= "d18"]:not([class $= "d18"]),
    [class *= "d19"]:not([class $= "d19"])
    {
        width: 150px !important;
    }
    
    /* weed out 200-299, making sure not to get 2 or 20-29
       NOTE: again high specificity
    */
    [class *= "d2"]:not([class $= "d2"]):not([class $= "d20"]):not([class $= "d21"]):not([class $= "d22"]):not([class $= "d23"]):not([class $= "d24"]):not([class $= "d25"]):not([class $= "d26"]):not([class $= "d27"]):not([class $= "d28"]):not([class $= "d29"])
    {
        width: 180px;
    }
    
    /* weed out 250-299, making sure not to get 25-29
       NOTE: !important needed again;
       also, anything 300+ reverts back to smallest size unless
       one keeps going... maybe 999 could be reached "reasonably"
    */
    [class *= "d25"]:not([class $= "d25"]),
    [class *= "d26"]:not([class $= "d26"]),
    [class *= "d27"]:not([class $= "d27"]),
    [class *= "d28"]:not([class $= "d28"]),
    [class *= "d29"]:not([class $= "d29"])
    {
        width: 210px !important;
    }
    

    【讨论】:

    • 这实际上是一个非常好的解决方案,最多 300 个就足以满足我的需求。谢谢!你是最有帮助的!
    【解决方案2】:

    如果不能以任何方式修改类,可以通过 CSS 属性选择器进行部分匹配:

    input[class^="txtField10"] /* catch txtField10, txtField100, txtField1000, etc */
    input[class^="txtField150"] /* catch txtField150, txtField15064, txtField150829, etc */
    

    我为示例选择了“begins with”语法,您可以在此处查看其他类型:http://css-tricks.com/attribute-selectors/

    字段的最大长度确实应该通过 maxlength 属性设置(您也可以像我上面所做的那样匹配)。但如果它是自动生成的,那么你就无能为力了。

    【讨论】:

      【解决方案3】:

      另一种方法是在您的对象上拥有多个类:

      <input type='text' id='txtField001' class='txtField width150'>foo</input> 
      

      或者更好的是一些不依赖于绝对实现的东西(这是我在内部开发框架中所做的,我有窄、宽等作为标准字段宽度):

      <input type='text' id='txtField001' class='txtField wide'>foo</input> 
      

      这样,如果您调整实现的细节,您就不会觉得需要调整每个类。

      此方法与脚本技术、生成的 CSS 文件或许多其他效率或一致性机制很好地结合在一起。

      要遵循上一个答案中的示例,使用 Dojo,因为我不是 jQuery 专家,因此您可以进行如下格式化:

      var widths = {
         narrow:  '10em';
         wide: '30em';
      }
      
      for (m : widths) {
         if widths.hasOwnProperty(m) {
            dojo.query('.' + m).style('width', widths[m]));
         } 
      }
      

      如果您绝对必须以各种样式对特定宽度进行编码,那么拥有一个额外的类来选择用于脚本的节点仍然会对您有所帮助。您将根据共享类进行选择,然后检查所有类以找到具有参数的类。

      如果您不遵守严格的验证合规性,您还可以将此信息嵌入自定义属性而不是类中,然后在 JavaScript 中对其进行解析。

      有很多方法。

      【讨论】:

      • 这是个好建议。如果没有人提出不同/替代方法,我可能会这样做。非常感谢!
      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2017-01-01
      • 2020-05-28
      相关资源
      最近更新 更多