HOME

浏览器行为

通过javascript,监听用户处理函数,动态添加的script 标签内的代码无法执行

        let button = document.querySelector('.btn')
        let container = document.querySelector('.container')
        // 注意要拼接位置,不然报错
        let literalString = "<script src='./test.js'>console.log('hhhh')</" + "script>";


        button.onclick = () => {
            container.innerHTML = literalString
            let oD = document.createElement('div')
            document.querySelector('body').append(oD)
        };