【问题标题】:javascript: css is not addressed, why?javascript: css 没有解决,为什么?
【发布时间】:2020-05-03 22:58:03
【问题描述】:

我写了以下函数:

createEl : function (type, id, className, parent, value) {
var el= document.createElement(type);
if(id)
    el.id = id;
if(className)
    el.className = className;
if(parent)
    parent.appendChild(el);
if(value)
    el.appendChild(document.createTextNode(value));
return el;
}

使用此代码:

var imageTD = this.createEl("td", null, "TD-IMAGE", bodyTR, bd);

css 已解决。但使用此代码:

var imageTD = this.createEl('td', null, "TD-IMAGE".concat(entrie.isBefore(now)?"_DIMMED":'', bodyTR, bd);

什么都没有发生。我做错了什么?这是完整的代码部分:

getDom: function() {
var wrapper = this.createEl("div", null, null, null, null);

// tell MM to call and get our content
Log.log(JSON.stringify(this.active_birthdays));

if ((moment() > this.midnight) || (!this.loaded)) {
    var month = moment().month();
    var year = moment().year();
    var monthName = moment().format("MMMM");
    var monthLength = moment().daysInMonth();
    var now = moment();

    if(Object.keys(this.active_birthdays).length > 0) {

    // create your table here
    var table = this.createEl("table", "birthday-table", "TABLE", wrapper, null);

    // create tableheader here, array of column names
    var table_header = this.createTableHeader(table, "THEAD", [" "," "]);

    // create TBODY section with day names
    var tBody = this.createEl('tBody', "birthday-tbody", "TBODY", table, null);

    var birthdays_seen = {};        
    for(var birthday of Object.keys(this.active_birthdays)) {
        for(var person of this.active_birthdays[birthday]) {

        // create looped row section
        var bodyTR = this.createEl('tr', "birthday-tr-body", "TR-BODY", tBody, null);

        let now = moment();
        let entrie = moment(birthday,"DD.MM");

        if(this.config.dimmEntries) {               
            entrie = moment(birthday,'DD.MM');
        }

        // delete leading 0 and month
        var bd = "";

        if(birthdays_seen[birthday] == undefined) {
            bd = (birthday.startsWith("0")? birthday.substring(1): birthday).split('.')[0];
            var imageTD = this.createEl('td', null, "TD-IMAGE".concat(entrie.isBefore(now)?"_DIMMED":'', bodyTR, bd);

            var nameTD = this.createEl("td", null, "TD-BODY".concat(entrie.isBefore(now)?"_DIMMED":'', bodyTR, person.name);

            this.createEl("span", null, "TD-AGE", nameTD, " ");                     

            // needs class for width
            var spanTDo = this.createEl("span", null, "TD-AGE".concat(entrie.isBefore(now)?"_DIMMED":'', nameTD, person.age);

        }
        else{
            // add a break
            this.createEl("br", null , null , spanTDo, null);
            // add a span with name
            var nameTD = this.createEl("span", null, "TD-BODY".concat(entrie.isBefore(now)?"_DIMMED":'',spanTDo, person.name);

            // add a span with age
            var spanTD = this.createEl("span", null, "TD-AGE".concat(entrie.isBefore(now)?"_DIMMED":'', spanTDo, person.age);

        }                   

        var spacerTR = this.createEl("tr", null, null , tBody, null);
        var spacerTD = this.createEl("td", null, "SPACER", spacerTR, " ");
        spacerTD.colSpan = "2";
        birthdays_seen[birthday] = true;

        }
    }

    // Create TFOOT section -- currently used for debugging only
    if (this.config.debugging) {
        var table_footer = this.createTableFooter(tBody, null, [" "," "]);
        //footerTD.innerHTML = "Birthdaylist is currently in DEBUG mode!<br />Please see console log.";
    }
    else {
        var table_footer = this.createTableFooter(tBody, null, [" "," "]);
    }

    }

    // pass the created content back to MM to add to DOM.
    return wrapper;

}
// Dom is loaded
this.loaded = true;

},

非常感谢。

【问题讨论】:

    标签: javascript css node.js function magic-mirror


    【解决方案1】:

    问题是您的父元素 bodyTR 未定义,bd 也是(来自您发布的代码库)

    如果您将代码替换为:

    function createEl (type, id, className, parent, value) {
      const el = document.createElement(type);
      if(id)
          el.id = id;
      if(className)
          el.className = className;
      if(parent)
          parent.appendChild(el);
      if(value)
          el.appendChild(document.createTextNode(value));
      return el;
    }
    
    const imageTD = this.createEl("td", null, "TD-IMAGE", document.body, 'test');
    
    console.log(imageTD)

    它的作品!

    专业提示:当你决定使用变量时使用 const/let 是当今的通用标准

    【讨论】:

    • 蠕虫在某处。不幸的是,一切都没有改变。那么这可能取决于我。你的代码在这里工作......
    • 问题出在你的父参数上。看看你的代码,当你创建 bodyTR 只是一个 DOM 节点时,你想在 imageTD 中使用它们吗?你应该用 document.querySelector(bodyTr) 声明它,如果它对你有帮助,你可以投票赞成这个线程:) 谢谢
    • 是的,但是使用 var imageTD = this.createEl('td', null, "TD-IMAGE".concat(entrie.isBefore(now)?"_DIMMED":'', bodyTR, bd ); 它不起作用。与部分 >>.concat(entrie.isBefore(now)?"_DIMMED":">this.createEl('td', null, "TD-IMAGE", bodyTR , bd);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2015-05-31
    • 2016-07-06
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多