【问题标题】:Dynamically add rules to CSS file动态添加规则到 CSS 文件
【发布时间】:2016-09-27 09:57:23
【问题描述】:

对于每个元素,我想动态地将规则添加到我的 CSS 中。每个规则应该有不同的背景图像和不同的名称。不知道我哪里做错了。我认为规则已添加到 CSS 文件中,但设置 ID 不会将动态添加的 div 与动态添加的 css 规则链接...

$(document).ready(function () {

var stylesheet = document.styleSheets[0];

var url = '/Home/PrikaziTipoveLokacija';

//this gets all the data from database and it works fine
$.ajax({
    type: "GET",
    url: url,
    cache: false,
    success: function (data) {
        //Checking number of rules in my CSS with this loop before adding new rules
        for (var i = 0; i < stylesheet.cssRules.length; i++) {
            count++;
        }
        alert(count); //this returns count 16 (CSS files has 16 rules)
        count = 0;            

        //this loop should add a rule to CSS for each element in data. I have set the same image for every element for now.
        for (elem in data) {
            if (stylesheet.addRule) {
                stylesheet.addRule(data[elem].Kljuc + '_ikona', '{ background-image: url("../Images/mesnicaicon.png"); background-size: cover; background-repeat:no-repeat;}');
            } else if (stylesheet.insertRule) {
                stylesheet.insertRule(data[elem].Kljuc + '_ikona' + '{' + 'background-image: url("../Images/mesnicaicon.png"); background-size: cover; background-repeat:no-repeat;' + '}', stylesheet.cssRules.length);
            }
        }

        //Checking number of rules in my CSS with this loop after adding new rules
        for (var i = 0; i < stylesheet.cssRules.length; i++) {
            count++;
        }
        alert(count); //this returns count 33, therefore , the new rules are added (Data has 17 elements, so, 33-16=17)
        count = 0;


        for (elem in data) {
            var div = document.createElement('div');


            div.className = 'ikona'; //irrelevant class
            div.id = data[elem].Kljuc + '_ikona'; //for each element set different id depending on the attribute 'Kljuc' from database

            var onclick = document.createAttribute("onclick");
            onclick.value = 'prikaziTipLokacije('+ data[elem].ID +')';
            div.setAttributeNode(onclick);

            div.innerHTML = data[elem].Naziv;

            document.getElementById('izbornik_za_lokacije').appendChild(div);
        }



    },
    error: function () {
        alert("Greška pri dohvaćanju tipova lokacija");
    }
});
});

甚至没有附加规则

【问题讨论】:

  • 您是否在新规则中添加了井号 # 以标识 id 值?
  • 是的,这就是问题所在。谢谢!

标签: javascript html css ajax rules


【解决方案1】:
var style = document.createElement('style');
style.innerHTML = '*{ color:red; } /* put your stuff here */';
document.body.appendChild(style);

创建新的样式元素并使用它

只需在 SO 上测试(从控制台运行)就可以了

document.styleSheets[0].insertRule('* { color: red; }')

【讨论】:

  • 感谢您的回答,它正在工作。但我仍然对向现有外部 CSS 文件添加规则时做错了什么感兴趣
  • 只需在 SO document.styleSheets[0].insertRule('* { color: red; }') 的控制台中测试它就可以了
猜你喜欢
  • 2015-06-30
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2014-08-09
相关资源
最近更新 更多