使用document.body.appendChild附加整个代码块

Using document.body.appendChild to append a whole code block?

本文关键字:代码 document body appendChild 使用      更新时间:2023-09-26

是否可以使用appendChild附加整个代码块?这就是我想附加的内容:

<script type="text/javascript" src="some.js"></script>
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="styles-ie.css" media="all" />
<![endif]-->
<!--[if lt IE 7]>
<script type="text/javascript" src="85d6d1c5f8f9c87ae016eae0ee6994c7.js"></script>
<![endif]-->

我想我不能用appendChild附加它?

如果你使用纯JavaScript,你可以使用这个:

var body = document.body;
body.innerHTML += 'Whatever you want to append.';
function addLABjs(callback) {
    var script = document.createElement("script");
    script.setAttribute("src", "YourOtherScript.js");
    script.addEventListener('load', function () {
        var script = document.createElement("script");
        script.textContent = "(" + callback.toString() + ")();";
        document.body.appendChild(script);
    }, false);
    document.body.appendChild(script);
}
// the guts of this userscript

function main() {
    $LAB.script("YourOtherScript.js").wait()
        .script("YourOtherScript.js")
        .script("YourOtherScript.js").wait();
    $(document).ready(function () {
    });
}
addLABjs(main);