简体   繁体   English

缺少样式的附加元素

[英]Appended element with missing styles

I'm trying to use the dynamic append with JavaScript, but the appended result is displayed without the main style.我正在尝试将动态附加与 JavaScript 一起使用,但显示的附加结果没有主样式。 I have tried to do some things advised on other questions, but none has generated a significant result, any suggestions?我已经尝试在其他问题上做一些建议,但没有产生显着的结果,有什么建议吗?

Links that did not help my problem:对我的问题没有帮助的链接:

Applying styles from CSS to newly appended elements 将样式从 CSS 应用到新添加的元素

Appending Child resets previous appended element value on JavaScript Appending Child 会在 JavaScript 上重置之前追加的元素值

Append Style to DOM not Replacing Existing 将样式附加到 DOM 不替换现有的

appending html with jquery but don't get style 用jquery附加html但没有样式

What I have:我拥有的:

 $(document).ready(function() { var max_fields = 4; var wrapper = $(".separator"); var add_button = $(".btn-x"); var x = 0; $(add_button).click(function(e) { e.preventDefault(); if (x < max_fields) { x++; $(wrapper).append(` <div class='tabbable paper-shadow relative' data-z='0.5' id='div'> <div class='tab-content'> <div id='course' class='tab-pane active'> <div class='form-group'> <textarea name='info${x}' id='info${x}' cols='30' rows='10' class='summernote'></textarea> </div> <div class='text-center'> <button type='button' class='delete'>-</button> </div> </div> </div> </div>`); // add input box } else { alert('You Reached the limits') } }); $(wrapper).on("click", ".delete", function(e) { e.preventDefault(); $(this).parent().parent().parent().parent().remove(); x--; }) });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tabbable paper-shadow relative" data-z="0.5"> <!-- Panes2 --> <div class="tab-content"> <div id="course" class="tab-pane active"> <div class="form-group"> <label for="info">Conteúdo</label> <textarea name="info" id="info" cols="30" rows="10" class="summernote"></textarea> </div> <div class="text-center"> <button type="button" class="btn-x">+</button> </div> </div> </div> <!-- // END Panes2 --> </div>

Thanks in advance :D提前致谢

Thanks to @Barmar for the help!感谢@Barmar 的帮助!

I solved the problem by first appending the .js files that were linked to the elements I was trying to append.我通过首先附加链接到我试图附加的元素的 .js 文件解决了这个问题。

Edited code:编辑的代码:

$(document).ready(function() {
       var max_fields      = 4;
       var wrapper         = $(".separator");
       var add_button      = $(".btn-x");

       var x = 0;
       $(add_button).click(function(e){
           e.preventDefault();
           if(x < max_fields){
               x++;

               var myfunc1 = document.createElement("script");
               myfunc1.type = "text/javascript";
               myfun1.src = "js/myfunctions1.js";
               $("head").append(myfunc1);
               var myfun2 = document.createElement("script");
               myfun2.type = "text/javascript";
               myfun2.src = "js/myfunctions2.js";
               $("head").append(myfunc2);

               wrapper.append(`<div class='tabbable paper-shadow relative' data-z='0.5' id='div'>
                                      <div class='tab-content'>
                                        <div id='course' class='tab-pane active'>
                                            <div class='form-group'>
                                              <textarea name='info${x}' id='info${x}' cols='30' rows='10' class='summernote'></textarea>
                                            </div>
                                            <div class='text-center'>
                                                <button type='button' class='delete'>-</button>
                                            </div>
                                        </div>
                                      </div>
                                    </div>`); //add input box
           } else {
                alert('You Reached the limits')
                }
       });

       wrapper.on("click",".delete", function(e){
           e.preventDefault(); 
           $(this).parent().parent().parent().parent().remove();
           x--;
       })
});

As far as I understand I needed to do this because the page only loads the functions for that custom textarea once.据我所知,我需要这样做,因为该页面只加载该自定义 textarea 的功能一次。 I think the problem was not related to CSS styles but to those functions that did not load for the appended elements.我认为问题与 CSS 样式无关,而是与那些未为附加元素加载的函数有关。

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

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