javascript - 为什么没有 a 而不是一个?
问题描述
我使用 JS 生成一个包含事物列表的侧边栏。问题是侧边栏只能在没有<!DOCTYPE html>
. 有一个,它需要元素列表的高度,如果没有一个,它需要身体的高度(这是我想要的)。这是代码(带<!DOCTYPE html>
):
<!DOCTYPE html>
<html>
<head>
<title>Why does this work without a doctype and not with one?</title>
<meta charset="UTF-8"/>
<style>
body{margin: 0; overflow-y: hidden; background-color: #aaaaaa}
.sidebar{width :18%; height: 100%; background-color: #333333;}
.lesson{background-color: #2fbfc0; padding: 5px; border-bottom: 2px solid #111111; font-size: 20px;}
</style>
</head>
<body>
<p>With <!DOCTYPE html></p>
<script>
var sidebar=document.createElement("div");
document.body.appendChild(sidebar);
sidebar.className="sidebar";
var parts="Intro\\index.html\nFirst\\first.html\nSecond\\second.html".split("\n");
var len=parts.length;
var i;
for(i=0;i<len;i++){
var section=document.createElement("div");
var segments=parts[i].split("\\");
segments[1]=segments[1].trim();
section.innerHTML=segments[0];
section.id=segments[1];
section.onclick=function(){location.assign(this.id);}
section.className="lesson";
if(segments[1]==(location.href.slice(-1)=="/"?location.href+"index.html":location.href).split("/").slice(-1)[0])
section.style.color="#3f5890";
sidebar.appendChild(section);
}
sidebar.childNodes[0].style.borderTop="2px solid #111111";
</script>
</body>
</html>
这是没有的代码<!DOCTYPE html>
:
<!--DOCTYPE html-->
<html>
<head>
<title>Why does this work without a doctype and not with one?</title>
<meta charset="UTF-8"/>
<style>
body{margin: 0; overflow-y: hidden; background-color: #aaaaaa}
.sidebar{width :18%; height: 100%; background-color: #333333;}
.lesson{background-color: #2fbfc0; padding: 5px; border-bottom: 2px solid #111111; font-size: 20px;}
</style>
</head>
<body>
<p>Without <!DOCTYPE html></p>
<script>
var sidebar=document.createElement("div");
document.body.appendChild(sidebar);
sidebar.className="sidebar";
var parts="Intro\\index.html\nFirst\\first.html\nSecond\\second.html".split("\n");
var len=parts.length;
var i;
for(i=0;i<len;i++){
var section=document.createElement("div");
var segments=parts[i].split("\\");
segments[1]=segments[1].trim();
section.innerHTML=segments[0];
section.id=segments[1];
section.onclick=function(){location.assign(this.id);}
section.className="lesson";
if(segments[1]==(location.href.slice(-1)=="/"?location.href+"index.html":location.href).split("/").slice(-1)[0])
section.style.color="#3f5890";
sidebar.appendChild(section);
}
sidebar.childNodes[0].style.borderTop="2px solid #111111";
</script>
</body>
</html>
(旁注:实际上,它没有一个名为 的固定数组parts
。在实际版本中,它使用 AJAX 请求来获取它。)
您可能会注意到,这两个片段之间只有两个区别:
第二个有 doctype 注释
第二个说
without
而不是with
看起来它没有显示 Stack Overflow 代码片段中的差异,但如果您复制代码并将其放在其他地方(例如设备上的 HTML 文件),它将无法正常工作。
这是复制并粘贴到您自己的文件中的代码:
<!DOCTYPE html> <!-- Remember to comment/uncomment to change it! -->
<html>
<head>
<title>Why does this work without a <!DOCTYPE html> and not with one?</title>
<meta charset="UTF-8"/>
<style>
body{margin: 0; overflow-y: hidden; background-color: #aaaaaa}
.sidebar{width :18%; height: 100%; background-color: #333333;}
.lesson{background-color: #2fbfc0; padding: 5px; border-bottom: 2px solid #111111; font-size: 20px;}
</style>
</head>
<body>
<p>With/Without <!DOCTYPE html></p>
<script>
var sidebar=document.createElement("div");
document.body.appendChild(sidebar);
sidebar.className="sidebar";
var parts="Intro\\index.html\nFirst\\first.html\nSecond\\second.html".split("\n");
var len=parts.length;
var i;
for(i=0;i<len;i++){
var section=document.createElement("div");
var segments=parts[i].split("\\");
segments[1]=segments[1].trim();
section.innerHTML=segments[0];
section.id=segments[1];
section.onclick=function(){location.assign(this.id);}
section.className="lesson";
if(segments[1]==(location.href.slice(-1)=="/"?location.href+"index.html":location.href).split("/").slice(-1)[0])
section.style.color="#3f5890";
sidebar.appendChild(section);
}
sidebar.childNodes[0].style.borderTop="2px solid #111111";
</script>
</body>
</html>
所以我的问题是,它为什么要这样做?对我来说,它似乎应该是相反的(使用 doctype应该让它工作,而不应该让它工作)。
解决方案
推荐阅读
- node.js - How to stub the same method in two test-cases using Sinon?
- arrays - Perl - 具有两个数组参数的函数
- java - JDBC ON DUPLICATE KEY UPDATE 不返回 0
- android - findViewById 在 PreferenceFragment 的 onViewCreated 中失败,Android
- azure-cognitive-search - Azure 搜索索引器失败 - “索引已停止,因为已达到免费技能组执行配额。”
- python-3.x - 奇怪的#HASHTAG 编码
- c# - 使用 Azure Devops API 创建生成定义
- reactjs - 使用 react-dropzone 的文件上传进度
- javascript - 如何解决 GTM 中 JavaScript 编译器错误的 RegExp 问题?
- angular - 从 mat-sidenav-container 外部的按钮打开 mat-sidenav