简体   繁体   English

使用JavaScript创建MDL复选框

[英]Creating MDL checkboxes with JavaScript

I'm working on a to-do list app. 我正在做一个待办事项清单应用程序。 User inputs task in a form, then JavaScript adds it to the HTML document. 用户以表格形式输入任务,然后JavaScript将其添加到HTML文档中。

I'm trying to use the Material Design Lite checkbox component in my list items, but the checkboxes won't render correctly. 我正在尝试在列表项中使用Material Design Lite复选框组件,但复选框无法正确呈现。 All other buttons and form inputs render fine. 所有其他按钮和表单输入都可以正常显示。 I've done my best to get the JavaScript to copy it exactly, but it seems like no matter what I do I can't get it to appear correctly. 我已经尽力让JavaScript准确地复制它,但是无论我做什么,似乎都无法正确显示它。

Check out the CodePen showing the problem here . 此处查看出现问题的CodePen。

I coded the first list item into the HTML to show the correct checkbox for reference. 我将第一个列表项编码到HTML中,以显示正确的复选框以供参考。 Adding subsequent list items through the form will show the incorrect checkbox. 通过表单添加后续列表项将显示错误的复选框。

JavaScript code: JavaScript代码:

    //create list item
    let newLi = document.createElement('li');  
    newLi.classList.add('mdl-list__item');

    //create primary span container
    let toDoContainer = document.createElement('span');
    toDoContainer.classList.add('mdl-list__item-primary-content');

    //create checkbox and attach to primary span container
    let toDoLabel = document.createElement('label');
    toDoLabel.classList.add('mdl-checkbox', 'mdl-js-checkbox', 'mdl-js-ripple-effect');
    toDoLabel.htmlFor = 'list-checkbox-1';
    let checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    checkbox.id = 'list-checkbox-1';
    checkbox.classList.add('mdl-checkbox__input');
    toDoLabel.appendChild(checkbox);
    toDoContainer.appendChild(toDoLabel);

    //create text and attach to primary span container
    let labelText = document.createTextNode(newToDoText.value);
    toDoContainer.appendChild(labelText);

    //append primary span container container to list item
    newLi.appendChild(toDoContainer);

    //create secondary span container
    let deleteContainer = document.createElement('span');
    deleteContainer.classList.add('mdl-list__item-secondary-action');

    //create delete button and append to secondary span container
    let deleteButton = document.createElement('button');
    deleteButton.classList.add('mdl-button', 'mdl-js-button', 'mdl-button--icon');
    let icon = document.createElement('i');
    let text = document.createTextNode('delete');
    icon.classList.add('material-icons');
    icon.appendChild(text);
    deleteButton.appendChild(icon);
    deleteContainer.appendChild(deleteButton);
    deleteContainer.addEventListener('click', function(e) {
        toDoList.removeChild(deleteButton.parentNode.parentNode);
    });

    //append secondary span container to list item
    newLi.appendChild(deleteContainer);

    //add list item to to-do list
    toDoList.appendChild(newLi);

    //empty form input
    newToDoText.value = '';

What the correct HTML should look like: 正确的HTML应该是什么样的:

<li class="mdl-list__item">
            <span class="mdl-list__item-primary-content">
                <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="list-checkbox-2">
                    <input type="checkbox" id="list-checkbox-2" class="mdl-checkbox__input" />
                </label>
                Finish this to-do list app
            </span>
            <span class="mdl-list__item-secondary-action">
                <button class="mdl-button mdl-js-button mdl-button--icon">
                    <i class="material-icons">delete</i>
                </button>
            </span>
 </li>

Anyone have an idea what I'm doing wrong? 有人知道我在做什么错吗?

MDL guidelines MDL准则

On every mdl elements that you insert, you should call componentHandler.upgradeElement(insertedElement) 在您插入的每个mdl元素上,都应调用componentHandler.upgradeElement(insertedElement)

The reason being material manipulates your dom to change into what it is and binds all events when you add material class to the element on initial load. 当您在初始加载时将材料类添加到元素时,材料的原因会操纵您的dom使其变为原来的形状并绑定所有事件。 When you append material elements later on, this doesnt happen, unless you explicitly specify to upgrade your added element to a material element. 以后添加实体元素时,除非您明确指定将添加的元素升级为实体元素,否则不会发生这种情况。

https://getmdl.io/started/index.html#dynamic https://getmdl.io/started/index.html#dynamic

componentHandler.upgradeElement(checkbox)
toDoLabel.appendChild(checkbox);
componentHandler.upgradeElement(toDoLabel)
toDoContainer.appendChild(toDoLabel);

EDIT You can alternatively add a class like "newMdl" on new material elements and then call componentHandler.upgradeElements(newMdlElements) 编辑您也可以在新的材质元素上添加类似“ newMdl”的类,然后调用componentHandler.upgradeElements(newMdlElements)

deleteContainer.classList.add('mdl-list__item-secondary-action', 'newMdl');
let deleteButton = document.createElement('button');
deleteButton.classList.add('mdl-button', 'mdl-js-button', 'mdl-button--icon', 'newMdl');
deleteContainer.appendChild(deleteButton);
newLi.appendChild(deleteContainer);
let mdlElemets = document.querySelectorAll(".newMdl");
componentHandler.upgradeElements(mdlElemets);

https://codepen.io/anon/pen/BbPRwq https://codepen.io/anon/pen/BbPRwq

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

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