20190527 HTML class属性 学习笔记

博客介绍了HTML属性的作用,即给相同值的元素赋予相同格式。还提到不同标签可指向同一class name,同一标签能指向多个class name,且class name大小写敏感,还可被Javascript引用实现特定功能,如隐藏特定格式元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML的<class>属性的作用是为有着相同’<class>' 值的元素赋予相同的格式。以下是一段定义<class>的代码。

<!doctype html>
<html>
<head>
<style>
.cities{
 color:white;
 background-color:tomato;
 text-align:left;
 margin:20px;
 padding:20px;
 }
 .main{
font-size:50%;
 }
 .Main{
 font-size:150%;
 }
</style>
</head>
<body>
<p class="cities">London</p>
<h2 class="cities"> Beijing</p>
<p class="cities main"> Tokoyo</p>
<p> This is <span class="main"> New York </span></p>
</body>
</html>
  1. 不同的标签都可以指向同一个 class name.
  2. 同一个标签可以同时指向多个class name
  3. Class name 是分大小写的。
  4. class name也可以被Javascript引用去实现特定功能。在学到Javascript的内容时可以再来看。其实是class name “city” 被"getElementsByClassName"方法引用,实现功能,当点击按钮时,可以隐藏所有带有“city"格式的元素。
<!DOCTYPE html>
<html>
<body>
    <h2>Using The class Attribute in JavaScript</h2>
    <p>Click the button, to hide all elements with the class name "city", with JavaScript:</p>    
    <button onclick="myFunction()">Hide elements</button>
   
    <h2 class="city">London</h2>
    <p>London is the capital of England.</p>
    
    <h2 class="city">Paris</h2>
    <p>Paris is the capital of France.</p>
    
    <h2 class="city">Tokyo</h2>
    <p>Tokyo is the capital of Japan.</p>
    
    <script>
    function myFunction() {
      var x = document.getElementsByClassName("city");
      for (var i = 0; i < x.length; i++) {
        x[i].style.display = "none";
      }
    }
    </script>
    </body>
    </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值