最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - dynamically built input using MDL does not render correctly - Stack Overflow

programmeradmin9浏览0评论

I have the following code,

var loginForm = document.createElement('div');

loginForm.className = 'row';

loginForm.innerHTML = '<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label textfield-demo"><input class="mdl-textfield__input" type="text" id="login" /><label class="mdl-textfield__label" for="login">Username</label></div>';

document.getElementById('page-content').appendChild(loginForm);

The problem is that since the javascript functions have already ran the input is not styled correctly.

Does anyone what javascript function I need to call to make this work? I tried MaterialTextfield.prototype.init() but nothing changed.

I have the following code,

var loginForm = document.createElement('div');

loginForm.className = 'row';

loginForm.innerHTML = '<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label textfield-demo"><input class="mdl-textfield__input" type="text" id="login" /><label class="mdl-textfield__label" for="login">Username</label></div>';

document.getElementById('page-content').appendChild(loginForm);

The problem is that since the javascript functions have already ran the input is not styled correctly.

Does anyone what javascript function I need to call to make this work? I tried MaterialTextfield.prototype.init() but nothing changed.

Share Improve this question asked Aug 10, 2015 at 2:47 Grady DGrady D 2,0399 gold badges33 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You can use MDL's upgrading element function. Since you have created the loginForm dynamically, you can upgrade it in scope with

    ponentHandler.upgradeElement(loginForm);
    //or, ponentHandler.upgradeDom(loginForm);
    //however, I suggest using jQuery to upgrade multiple if you are adding more than one
    ponentHandler.upgradeElements($('.mdl-textfield').get());

This will get all the mdl-textfield objects and upgrade them (if not upgraded before)

I kept digging through the source code and found ponentHandler.upgradeDom(). When running this function all of the dynamic elements are fixed.

发布评论

评论列表(0)

  1. 暂无评论