简体   繁体   English

如何动态创建CSS样式?

[英]How can I create CSS styles dynamically?

In this jQuery example: http://jqueryui.com/demos/sortable/#empty-lists , they have CSS tags for each of the sortable ul's (sortable1, 2, and 3) like so: 在此jQuery示例中: http : //jqueryui.com/demos/sortable/#empty-lists ,它们为每个可排序ul(sortable1、2和3)具有CSS标记,如下所示:

CSS 的CSS

#sortable1 li, #sortable2 li, #sortable3 li { 
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}​

HTML 的HTML

<div class="demo">

<ul id="sortable1" class='droptrue'>
    (...)
</ul>

<ul id="sortable2" class='dropfalse'>
    (...)
</ul>

<ul id="sortable3" class='droptrue'>
</ul>

The problem I'm having is that in my web page, I don't know ahead of time how many ul's i'll have. 我遇到的问题是,在我的网页中,我提前不知道我将拥有多少ul。 What's the best way to handle this without defining a every iteration of #sortable? 没有定义#sortable的每个迭代的最佳方法是什么?

You actually don't need to use separate ids for the CSS, you can use a single class instead: 实际上,您实际上不需要为CSS使用单独的ID,而可以使用单个类:

CSS 的CSS

.sortable li { 
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}​

HTML 的HTML

<div class="demo">

<ul id="sortable1" class='sortable droptrue'>
    (...)
</ul>

<ul id="sortable2" class='sortable dropfalse'>
    (...)
</ul>

<ul id="sortable3" class='sortable droptrue'>
</ul>

The droptrue and dropfalse classes will still also work as expected. droptruedropfalse类仍将按预期工作。

People seem to be forgetting the simple CSS3 method: 人们似乎忘记了简单的CSS3方法:

ul[id^=sortable] li {
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}

We use the Attribute Starts With selector here. 我们在这里使用“属性起始于”选择器。 So this applies styling to any li elements inside any ul whose id attribute starts with the term sortable . 因此,这将样式应用于任何ul id属性以sortable开头的任何li元素。

Example . 例子

@AndrewBarber's answer is the better way to approach things, but for the record, you can create CSS dynamically. @AndrewBarber的答案是更好的方法,但是根据记录,您可以动态创建CSS。

What you need to do is use some kind of server-side language (PHP is most popular, for whatever that's worth; ASP.NET, JSP, etc. are also used) to generate the CSS on the fly. 您需要做的是使用某种服务器端语言(PHP最受欢迎,无论价格如何;还使用ASP.NET,JSP等)来动态生成CSS。

For example, you could use this HTML: 例如,您可以使用以下HTML:

<link rel="stylesheet" type="text/css" href="stylesheet.php">

And then have this for stylesheet.php : 然后将其用于stylesheet.php

<?php
    header('Content-Type: text/css');
    for ( $i = 1; $i < $sortableMax; ++$i )
    {
        echo <<<EOT
#sortable$i
{
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}
EOT;
    }
?>

This produces the following CSS, assuming $sortableMax was somehow set to 5 : 假设$sortableMax设置为5 ,这将产生以下CSS:

#sortable1
{
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}
#sortable2
{
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}
#sortable3
{
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}
#sortable4
{
    margin: 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
}

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

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