码迷,mamicode.com
首页 > Web开发 > 详细

html中id值必须唯一吗?

时间:2015-09-07 12:44:17      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

HTML实验:

1.新建网页,输入一下内容,在浏览器打开

<html>
    <head>
        <meta http-equiv="content-type" charset="utf-8"/>
    </head>
    <body>
        姓名:<input type="text" id="inTxt"/><br/>
        密码:<input type="text" id="inTxt"/>
    </body>
</html>

2.效果图  

技术分享

两个输入框使用了相同的id号,但能正常显示,浏览器未报错

CSS实验

1.修改源代码,添加样式

<html>
    <head>
        <meta http-equiv="content-type" charset="utf-8"/>
        
        <style>
            #inTxt
            {
                background-color:red;    
            }
        </style>
    </head>
    <body>
        姓名:<input type="text" id="inTxt"/><br/>
        密码:<input type="text" id="inTxt"/>
    </body>
</html>

2.效果图

技术分享

JS实验

1.修改源代码,添加脚本

<html>
    <head>
        <meta http-equiv="content-type" charset="utf-8"/>
        
        <style>
            #inTxt
            {
                background-color:red;    
            }
        </style>
    </head>
    <body>
        姓名:<input type="text" id="inTxt" value="a"/><br/>
        密码:<input type="text" id="inTxt" value="b"/>
        
        <script>
            var input = document.getElementById("inTxt");
            alert(input.value);
        </script>
    </body>
</html>

2.效果图

技术分享

 

总结

1.html中有相同的id号,不影响显示

2.css对页面中有相同id号的标签都会应用样式

3.多个相同的id号,js只去第一个

 

html中id值必须唯一吗?

标签:

原文地址:http://www.cnblogs.com/pmx-pmx/p/4788484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!