Fiddle: https://jsfiddle.net/x9ghz1ko/(附有注释。)
我有一个HTML文档,其中包含两个部分:
我的代码简化版:(...不起作用。)
也许一个解决方案是:我有一个空的
我有一个HTML文档,其中包含两个部分:
<section class="desktop_only">
和<section class="mobile_only">
- 在代码中,这些部分之前有一个脚本,该脚本应该删除其中一个部分,但问题是:为了实现此目的,我必须将脚本放在这些部分之后,但我不能这样做,因为在这些部分内还有更多的脚本,并且我必须在它们运行之前将它们删除。我的代码简化版:(...不起作用。)
<script>
device = "desktop"; // This could be either: "desktop", or: "mobile"
if(device === "desktop"){
document.querySelector(".mobile_only").remove(); // Not doing anything.
} else {
document.querySelector(".desktop_only").remove(); // Not doing anything.
}
</script>
<section class="desktop_only">
<script src="example.js"></script>
<script> console.log("desktop_only") </script>
<div> desktop </div>
</section>
<section class="mobile_only">
<script src="example.js"></script>
<script> console.log("mobile_only") </script>
<div> mobile (Delete this.) </div>
</section>
,称之为:
".platform_dependent"
,并且我有两个外部文件:desktop_only.something
和 mobile_only.something
,然后我将其中一个文件的内容加载到:platform_dependent.innerHTML
中...但我不知道如何实现?
desktop_only
和mobile_only
中的所有脚本都将运行,然后我将删除它们,但它们已经被执行了 - 这就是问题所在。 - Simon R.<script>
写在HTML代码中间是毫无意义的,应该全部放在<head>或<body>的末尾。在HTML中间写它只会制造混乱。 - Jeremy Thille