页面的兼容性从认识<!DOCTYPE>开始

本文深入探讨了<!DOCTYPE>标记在网页布局、浏览器兼容性和代码验证方面的重要性。解释了不同<!DOCTYPE>类型对浏览器展示效果的影响,并强调了在项目中忽视<!DOCTYPE>可能导致的兼容性问题。

今天为了css的布局的游览器兼容性调整了一晚上,怎么调怎么不对,四处找资料和解决方案,无意中发现通过IDE创建的页面没这个兼容性问题,然后我就不停找原因和缩小范围。最后,发现只发现<!DOCTYPE>有所区别,这是我以前写网页是很火大的一段文字,每次会情不自禁的删掉,经过研究后发现,千万不能小看<!DOCTYPE>

当您打开网易,新浪等网站的任何一个页面的源码时,第一行都会看到<!DOCTYPE....dtd>,很多人不明白这段话什么意思,有什么作用。而在自己做项目的时候根本不在乎<!DOCTYPE>,甚至特意删除<!DOCTYPE>,这里特别提醒您:您删除<!DOCTYPE>的时刻,正是灾难开始发生的时刻。 


<!DOCTYPE>在遵循标准的任何Web文档中,它都是一项必需的元素。<!DOCTYPE>会影响代码验证,并决定了浏览器最终如何显示你的Web文档。现在您明白了吧,当你删除了<!DOCTYPE>时,就是把如何展示您html页面的权利给浏览器!很恐怖吧,IE6,IE7,IE8,Firefox2,Firefox3,Chrome,有多少种浏览器,您的页面可能就会产生多少种展示方式。 

如果您的页面根本就没有<!DOCTYPE>,那么您考虑浏览器的兼容性,就是缘木求鱼,提高页面的兼容性从重视<!DOCTYPE>开始。 

能够见到的<!DOCTYPE>有以下这几种: 

HTML 2 
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
HTML 3.2 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
HTML 4.01 Strict, Transitional, Frameset 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> 
XHTML 1.0 Strict, Transitional, Frameset 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 
XHTML 1.1 xhtml1.1,XHTML 1.1 plus MathML 2.0 plus SVG 1.1 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"> 

我们常用的有: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

xhtml1-strict:声明文档中不使用任何失效属性以及框架 
loose:声明文档使用一些失效元素包括显示 
xhtml1-transitional:声明文档使用一些失效元素并且使用框架 

建议您的页面都是用strict声明,这样可以最大限度的提高页面的兼容性,不过这样可能会造成您在编写html程序时必须非常严格。只有当strict声明太严格,给您编码和实现造成了困难,您可以使用transitional声明,否则坚持使用strict。 

最后再次声明: 
您删除<!DOCTYPE>的时刻,正是灾难开始发生的时刻。提高页面的兼容性从认识<!DOCTYPE>开始
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>给朋友的悄悄话 - FLAC音乐版</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Arial', sans-serif; } body { min-height: 100vh; display: flex; justify-content: center; align-items: center; background: linear-gradient(45deg, #ff7e5f, #feb47b, #86a8e7, #91eae4); background-size: 400% 400%; animation: gradientShift 15s ease infinite; position: relative; overflow-x: hidden; } /* 动态背景动画 */ @keyframes gradientShift { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } /* 浮动气泡效果 */ .bubble { position: absolute; border-radius: 50%; background: rgba(255, 255, 255, 0.2); animation: float 15s infinite linear; z-index: -1; } @keyframes float { 0% { transform: translateY(100vh) scale(0); opacity: 0; } 10% { opacity: 0.7; } 90% { opacity: 0.3; } 100% { transform: translateY(-100px) scale(1.2); opacity: 0; } } .container { background-color: rgba(255, 255, 255, 0.9); border-radius: 20px; box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2); padding: 40px; width: 90%; max-width: 600px; text-align: center; z-index: 1; backdrop-filter: blur(10px); } h1 { color: #333; margin-bottom: 10px; font-size: 2.5rem; } .subtitle { color: #666; margin-bottom: 30px; font-size: 1.2rem; } .search-container { position: relative; margin-bottom: 30px; } #searchInput { width: 100%; padding: 15px 20px; font-size: 1.1rem; border: 2px solid #ddd; border-radius: 50px; outline: none; transition: all 0.3s; } #searchInput:focus { border-color: #86a8e7; box-shadow: 0 0 10px rgba(134, 168, 231, 0.5); } #searchBtn { position: absolute; right: 5px; top: 5px; background: linear-gradient(45deg, #86a8e7, #91eae4); border: none; color: white; padding: 10px 25px; border-radius: 50px; cursor: pointer; font-size: 1rem; transition: all 0.3s; } #searchBtn:hover { transform: scale(1.05); box-shadow: 0 5px 15px rgba(134, 168, 231, 0.4); } .message-container { background: white; border-radius: 15px; padding: 25px; margin-top: 20px; min-height: 150px; display: flex; align-items: center; justify-content: center; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); border-left: 5px solid #86a8e7; opacity: 0; transform: translateY(20px); transition: all 0.5s; } .message-container.show { opacity: 1; transform: translateY(0); } .message-text { font-size: 1.3rem; color: #333; line-height: 1.6; } .music-controls { margin-top: 20px; display: flex; justify-content: center; align-items: center; gap: 10px; } .music-btn { background: rgba(255, 255, 255, 0.7); border: none; border-radius: 50%; width: 50px; height: 50px; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; transition: all 0.3s; } .music-btn:hover { background: white; transform: scale(1.1); } .music-info { margin-top: 10px; font-size: 0.9rem; color: #666; } footer { margin-top: 30px; color: #666; font-size: 0.9rem; } /* 响应式设计 */ @media (max-width: 768px) { .container { padding: 25px; } h1 { font-size: 2rem; } #searchInput { padding: 12px 15px; font-size: 1rem; } .message-text { font-size: 1.1rem; } } </style> </head> <body> <!-- 动态背景气泡 --> <div id="bubbles"></div> <!-- 主要内容容器 --> <div class="container"> <h1>给朋友的悄悄话</h1> <p class="subtitle">输入朋友的名字,查看你想对他/她说的话</p> <!-- 搜索区域 --> <div class="search-container"> <input type="text" id="searchInput" placeholder="请输入朋友的名字..."> <button id="searchBtn">搜索</button> </div> <!-- 留言显示区域 --> <div class="message-container" id="messageContainer"> <p class="message-text" id="messageText">请输入朋友的名字查看留言</p> </div> <!-- 音乐控制 --> <div class="music-controls"> <button class="music-btn" id="toggleMusicBtn">⏸</button> <button class="music-btn" id="muteBtn">🔊</button> </div> <div class="music-info">支持FLAC无损音质 | 当前播放: 背景音乐</div> <footer> <!-- 音乐播放器 (支持FLAC格式) --> <audio id="bgMusic" loop> <!-- 重要:请将您的FLAC音乐文件命名为 background-music.flac 并放在与HTML文件相同的目录 --> <source src="background-music.flac" type="audio/flac"> <!-- 为了更好的浏览器兼容性,建议同时提供MP3版本作为备选 --> <source src="background-music.mp3" type="audio/mpeg"> 您的浏览器不支持音频播放功能,请升级浏览器。 </audio> <p>温馨提醒:FLAC无损音乐已就绪,可点击上方按钮控制</p> </footer> </div> <script> // 创建动态背景气泡 function createBubbles() { const bubblesContainer = document.getElementById('bubbles'); const bubbleCount = 15; for (let i = 0; i < bubbleCount; i++) { const bubble = document.createElement('div'); bubble.classList.add('bubble'); // 随机大小和位置 const size = Math.random() * 100 + 20; const left = Math.random() * 100; const delay = Math.random() * 20; const duration = Math.random() * 10 + 15; bubble.style.width = `${size}px`; bubble.style.height = `${size}px`; bubble.style.left = `${left}%`; bubble.style.animationDelay = `${delay}s`; bubble.style.animationDuration = `${duration}s`; bubblesContainer.appendChild(bubble); } } // 朋友名字和对应留言的数据 const friendMessages = { "张三": "张三,认识你是我最大的幸运!希望我们的友谊长存!", "李四": "李四,谢谢你一直以来的支持与陪伴,你是我最好的朋友!", "王五": "王五,无论未来如何,我都会在你身边支持你!", "赵六": "赵六,你的笑容总能给我力量,愿你永远开心!", "小明": "小明,感谢你在我困难时伸出援手,你是我真正的朋友!", "小红": "小红,你的善良和热情感染着每一个人,继续保持!", "小李": "小李,和你在一起的每一天都充满欢乐,谢谢你的陪伴!", "小张": "小张,愿我们的友谊像美酒,越陈越香!", "测试": "这是一个测试消息,网站功能正常!FLAC音乐播放正常!" // 您可以在这里添加更多朋友和留言 }; // 搜索功能 document.getElementById('searchBtn').addEventListener('click', searchMessage); document.getElementById('searchInput').addEventListener('keypress', function(e) { if (e.key === 'Enter') { searchMessage(); } }); function searchMessage() { const name = document.getElementById('searchInput').value.trim(); const messageContainer = document.getElementById('messageContainer'); const messageText = document.getElementById('messageText'); if (name === '') { messageText.textContent = '请输入朋友的名字'; messageContainer.classList.add('show'); return; } if (friendMessages[name]) { messageText.textContent = friendMessages[name]; } else { messageText.textContent = `抱歉,没有找到关于"${name}"的留言。您可以尝试其他名字如:${Object.keys(friendMessages).join('、')}`; } messageContainer.classList.add('show'); } // 音乐控制功能 (支持FLAC) const bgMusic = document.getElementById('bgMusic'); const toggleMusicBtn = document.getElementById('toggleMusicBtn'); const muteBtn = document.getElementById('muteBtn'); // 音频加载状态检查 bgMusic.addEventListener('loadeddata', function() { console.log('FLAC音频文件加载完成,可以播放'); }); bgMusic.addEventListener('error', function(e) { console.error('音频加载错误:', e); alert('音乐文件加载失败,请检查文件名和路径是否正确。确保文件名为 background-music.flac 并位于正确目录。'); }); // 合并播放/暂停按钮功能 toggleMusicBtn.addEventListener('click', function() { if (bgMusic.paused) { bgMusic.play().then(() => { toggleMusicBtn.textContent = '⏸'; // 暂停图标 }).catch(e => { console.error('播放失败:', e); }); } else { bgMusic.pause(); toggleMusicBtn.textContent = '▶'; // 播放图标 } }); // 静音按钮功能 let isMuted = false; muteBtn.addEventListener('click', () => { isMuted = !isMuted; bgMusic.muted = isMuted; muteBtn.textContent = isMuted ? '🔇' : '🔊'; }); // 初始化 - 尝试自动播放音乐 window.onload = function() { createBubbles(); // 尝试自动播放音乐 const playPromise = bgMusic.play(); if (playPromise !== undefined) { playPromise.then(() => { // 自动播放成功 console.log('音乐自动播放成功'); toggleMusicBtn.textContent = '⏸'; // 显示暂停图标 }).catch(error => { // 自动播放被阻止,需要用户交互 console.log('自动播放被阻止,需要用户交互:', error); toggleMusicBtn.textContent = '▶'; // 显示播放图标 // 添加一次性点击事件,允许用户通过点击按钮开始播放 document.addEventListener('click', function initMusic() { bgMusic.play().then(() => { toggleMusicBtn.textContent = '⏸'; }); document.removeEventListener('click', initMusic); }, { once: true }); }); } // 显示默认消息 setTimeout(() => { document.getElementById('messageContainer').classList.add('show'); }, 500); // 控制台提示 console.log('网站已加载,支持FLAC无损音频播放'); console.log('请确保 background-music.flac 文件存在于同一目录下'); }; </script> </body> </html>请帮我修改这段代码实现一个功能:进入网站自动开始播放音乐
最新发布
11-04
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值