简体   繁体   English

用javaScript注入的html文本输入将忽略onclick材质设计css

[英]html text input injected with javaScript is ignoring onclick material design css

I am doing a templating platform and I am using material design for the looks. 我正在做一个模板平台,并且正在使用外观设计材料。 I just started coding the part where inputs are injected to the form. 我刚刚开始编码将输入注入表单的部分。 The thing is that I just started injecting text inputs and it looks as it should but the onclick css is not working! 事情是,我刚刚开始注入文本输入,看起来像应该的一样,但是onclick css无法正常工作! (The onclick css makes a blue line expand from the center of the text input, telling that the input is in focus) (onclick css使蓝线从文本输入的中心开始扩展,表明输入已对准焦点)

This is how I am injecting the html: 这就是我注入html的方式:

document.getElementById("smallTextField").onclick = function () {
let div = document.getElementById("formContainer")
div.insertAdjacentHTML("afterbegin", '<div class="col-sm-3">\n' +
    '                                    <div class="form-group">\n' +
    '                                        <div class="form-line" id="test">\n' +
    '                                            <input type="text" class="form-control" placeholder="col-sm-3">\n' +
    '                                        </div>\n' +
    '                                    </div>\n' +
    '                                </div>');}

I hope you can understand my problem :/ 希望您能理解我的问题:/

If you are able to use jQuery, one option you have is to utilize the $element.prepend() function after binding to the click event. 如果您能够使用jQuery,则必须选择的一种方法是在绑定到click事件之后利用$element.prepend()函数。

 $('#smallTextField').on('click', function () { let $div = $('#formContainer'); $div.prepend( '<div class="col-sm-3">\\n' + ' <div class="form-group">' + ' <div class="form-line" id="test">' + ' <input type="text" class="form-control" placeholder="col-sm-3" />' + ' </div>' + ' </div>' + '</div>'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="smallTextField" placeholder="smallTextField" /> <div id="formContainer"></div> 

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

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