Can't append [removed] element

后端 未结 18 2270
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 06:11

Any idea why the piece of code below does not add the script element to the DOM?

var code = \"\";
$(\"#someElement\").append(cod         


        
18条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:26

    I wrote an npm package that lets you take an HTML string, including script tags and append it to a container while executing the scripts

    Example:

    import appendHtml from 'appendhtml';
    
    const html = '

    Hello

    '; const container = document.getElementById('some-div'); await appendHtml(html, container); // appendHtml returns a Promise, some_js_file.js is now loaded and executed (note the await)

    Find it here: https://www.npmjs.com/package/appendhtml

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题