简体   繁体   English

JavaScript create元素在ES6类中不起作用

[英]JavaScript create element not working in ES6 Classes

Ive been playing around with ES6 classes trying to get a better understanding of how to use them by building a simple MVC todo app. 我一直在玩ES6类,试图通过构建一个简单的MVC todo应用程序更好地了解如何使用它们。 I ran into an issue that I have not been able to understand. 我遇到了一个我无法理解的问题。 I have an app class that instantiates the other classes. 我有一个实例化其他类的应用程序类。 The template class is used to build all the DOM elements which is does, but the DOM elements are not working as expected. 模板类用于构建所有的DOM元素,但是DOM元素无法正常工作。 For example the button and icon font will not show after the elements are added to the DOM. 例如,将元素添加到DOM后,按钮和图标字体将不会显示。

The Main Application Class: 主要应用类别:

import Template from './app/template';
import Model from './app/model';
import View from './app/view';
import Controller from './app/controller';

import Utils from './app/services/utils';

export default class App {
  constructor(config) {
    const id = config.id || 'todo';
    const cont = Utils.id(id);

    const template = new Template(cont);
    const model = new Model();
    const view = new View();
    this.controller = new Controller(model, view);
  }
}

var app = new App({
  id: 'app'
});

The Template Class: 模板类:

import Utils from './services/utils';

export default class Template {
  constructor(domEl) {
    this.container = domEl;

    this.init();
  }

  init() {
    this.container.className = 'todo-container widget';

    let header = Utils.dom('header', {
      parent: this.container
    });

    let inputCont = Utils.dom('input', {
      parent: header,
      className: 'input-container',
    });

    let input = Utils.dom('input', {
      parent: inputCont,
      className: 'input',
      attributes: [
        { att: 'type', val: 'text' },
        { att: 'placeholder', val: 'Add a task...' }
      ]
    });

    let btnAdd = Utils.dom('button', {
      parent: inputCont,
      className: 'btn-add'
    });

    let btnAddIcon = Utils.dom('i', {
      parent: btnAdd,
      className: 'far fa-check-circle'
    });

    let todos = Utils.dom('ul', {
      parent: this.container,
      className: 'todos'
    });

  }
}

The Template Class uses a group of simple helper methods: 模板类使用一组简单的辅助方法:

const Utils = {

  qs: (selector, scope) => {
    return (document || scope).querySelector(selector);
  },

  id: (id) => {
    return document.getElementById(id);
  },

  $on: (target, type, callback, capture) => {
    return target.addEventListener(type, callback, !!capture);
  },

  dom: (name, options) => {
    let element = document.createElement(name);

    if (options.parent) {
      options.parent.appendChild(element);
    }

    if (options.text) {
      element.innerHTML = options.text;
    }

    if (options.className) {
      element.className = options.className;
    }

    if (options.id) {
      element.id = options.id;
    }

    if (options.attributes) {
      options.attributes.forEach(attr => {
        let att = attr['att'];
        let val = attr['val'];
        element.setAttribute(att, val);
      });
    }

    return element;
  }
};

export default Utils;

When the code is ran the elements can be found in the inspector: 运行代码后,可以在检查器中找到这些元素:

在此处输入图片说明

But the elements are not working: 但是这些元素不起作用:

在此处输入图片说明

Manually adding the html I generated dynamically shows the proper results: 手动添加动态生成的html可显示正确的结果:

在此处输入图片说明

I have attempting to Google the issue and found nothing. 我试图用Google搜索该问题,但未发现任何问题。 I have also taken the code in the Template init function and added it to the constructor which gives the same results. 我还采用了模板init函数中的代码,并将其添加到提供相同结果的构造函数中。

Thanks in advance. 提前致谢。

You're making your markup the child of an input element, causing it not to display properly. 您正在将标记作为input元素的子元素,从而导致其无法正确显示。 Try making inputCont a div instead. 尝试改为将inputCont div。

let inputCont = Utils.dom('div', {
  parent: header,
  className: 'input-container',
});

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

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