html中的唯一id值 [英] unique id value in html

查看:189
本文介绍了html中的唯一id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题更多是出于好奇,而不是被卡住。我知道在HTML ID需要是唯一的,我可以看到在HTML验证中的错误。然而,浏览器愉快地将样式表应用于这两个元素。是因为他们在解析还是比较放松?这意味着我可以使用这样的造型等,或者它有副作用。

 <!DOCTYPE HTML PUBLIC -  / / W3C // DTD HTML 4.01 // ENhttp://www.w3.org/TR/html4/strict.dtd\"> 
< html lang =en>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title>< / title>

< style type =text / css>
#abc {color:red; }
< / style>
< / head>
< body>
< div id =abc> Hello Div 1< / div>
< div id =abc> Hello Div 2< / div>
< / body>
< / html>

在浏览器中输出。

解决方案

加载一个错误的HTML页面,它会一直在死亡。而且你不会想到很多的浏览器。



浏览器尽可能地宽容,尽可能地使用它,这是非常重要的。由于这个原因,所有的浏览器在处理标记方面往往是非常宽松的。



当然,这并不意味着你不应该写有效的HTML。 p>

This is a question more out of curiosity rather than being stuck. I know in html id needs to be unique and I can see the error in html validation. Yet the browsers happily apply style sheets to both elements. Is it because they are more relaxed in parsing or what. Does that mean I can use this for styling etc or does it have side effects.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>

<style type="text/css">
  #abc { color: red; }
</style>
</head>
<body>
  <div id="abc">Hello Div 1</div>
  <div id="abc">Hello Div 2</div>
</body>
</html>

Output in browsers.

解决方案

If your browser died every time it loaded an HTML page with errors, it'd be dying all the time. And you wouldn't think very much of the browser.

It's important for a browser to be as forgiving as possible and work as best it can with what it has. For this reason, all browsers tend to be very lenient in how they handle markup.

Of course, that doesn't mean you shouldn't write valid HTML.

这篇关于html中的唯一id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆