软件教程 2025年08月6日
0 收藏 0 点赞 228 浏览 2637 个字
摘要 :

文章目录 HTML 中的类属性 定义 HTML 类 使用不同类名的另一个示例 JavaScript 中的类属性 多个类 相同类名不同标签 HTML 中的类属性 HTML 类属性用于指定 HTML 元素的……




  • HTML 中的类属性
  • 定义 HTML 类
  • 使用不同类名的另一个示例
  • JavaScript 中的类属性
  • 多个类
  • 相同类名不同标签

HTML 中的类属性

HTML 类属性用于指定 HTML 元素的一个或多个类名。类名可以被 CSS 和 JavaScript 用来执行一些与 HTML 元素相关的任务。您可以在 CSS 中使用该类,只需写一个句点 (.),然后跟上类名以选择元素。

类属性可以在 <style> 标签内定义,也可以在单独的文件中使用句点 (.) 字符定义。

在 HTML 文档中,可以在不同元素中使用相同的类属性名称。

定义 HTML 类

要创建一个 HTML 类,首先使用以下示例在 <head> 部分内定义 HTML 类的样式:<style> 标签:

示例:

<head>  
    <style>  
        .headings{   
            color: lightgreen;  
            font-family: cursive;  
            background-color: black; }  
    </style>  
</head>  

我们为类名 “headings” 定义了样式,然后可以在任何我们想要提供此样式的 HTML 元素中使用这个类名。只需遵循以下语法来使用它。

<tag class=\"ghf\"> content </tag>  

示例 1:

<!DOCTYPE html>  
<html>  
<head>  
    <style>  
        .headings{   
            color: lightgreen;  
            font-family: cursive;  
            background-color: black; }  
    </style>  
</head>  
<body>  
<h1 class=\"headings\">This is first heading</h1>  
<h2 class=\"headings\">This is Second heading</h2>  
<h3 class=\"headings\">This is third heading</h3>  
<h4 class=\"headings\">This is fourth heading</h4>  
</body>  
</html>  

HTML class类属性

使用不同类名的另一个示例

示例:让我们使用类名 “Fruit” 来为所有元素设置 CSS 样式。

<style>    
.fruit {    
    background-color: orange;    
    color: white;    
    padding: 10px;    
}     
</style>    
    
<h2 class=\"fruit\">Mango</h2>    
<p>Mango is king of all fruits.</p>    
    
<h2 class=\"fruit\">Orange</h2>    
<p>Oranges are full of Vitamin C.</p>    
    
<h2 class=\"fruit\">Apple</h2>    
<p>An apple a day, keeps the Doctor away.</p>    

HTML class类属性

在这里,您可以看到我们使用了类名 “fruit” 与 (.) 来使用它的所有元素。

注意:您可以在任何 HTML 元素上使用类属性。类名是区分大小写的。

JavaScript 中的类属性

您可以使用 JavaScript 使用 getElementsByClassName() 方法来访问具有指定类名的元素。

示例:

<!DOCTYPE html>    
<html>    
<body>    
    
<h2>Class Attribute with JavaScript</h2>    
<p>Click the button, to hide all elements with the class name \"fruit\", with JavaScript:</p>    
    
<button onclick=\"myFunction()\">Hide elements</button>    
    
    
<h2 class=\"fruit\">Mango</h2>    
<p>Mango is king of all fruits.</p>    
    
<h2 class=\"fruit\">Orange</h2>    
<p>Oranges are full of Vitamin C.</p>    
    
<h2 class=\"fruit\">Apple</h2>    
<p>An apple a day, keeps the Doctor away.</p>    
    
<script>    
function myFunction() {    
  var x = document.getElementsByClassName(\"fruit\");    
  for (var i = 0; i < x.length; i++) {    
    x[i].style.display = \"none\";    
  }    
}    
</script>    
    
</body>    
</html>    

当用户单击按钮时,让我们隐藏所有带有类名 “fruit” 的元素。注意:您将在我们的 JavaScript 教程中了解更多关于 JavaScript 的内容。

多个类

您可以在 HTML 元素中使用多个类名(不止一个)。这些类名必须用空格分隔。

示例:

<!DOCTYPE html>    
<html>    
<style>    
.fruit {    
    background-color: orange;    
    color: white;    
    padding: 10px;    
}     
    
.center {    
    text-align: center;    
}    
</style>    
<body>    
    
<h2>Multiple Classes</h2>    
<p>All three elements have the class name \"fruit\". In addition, Mango also have the class name \"center\", which center-aligns the text.</p>    
    
<h2 class=\"fruit center\">Mango</h2>    
<h2 class=\"fruit\">Orange</h2>    
<h2 class=\"fruit\">Apple</h2>    
    
</body>    
</html>  

HTML class类属性

让我们为类名 “fruit” 和类名 “center” 的元素设置样式。您可以看到第一个元素 <h2> 同时属于 “fruit” 类和 “center” 类。

相同类名不同标签

您可以在不同标签上使用相同的类名,如 <h2> 和 <p> 等,以共享相同的样式。

示例:

<!DOCTYPE html>    
<html>    
<style>    
.fruit {    
  background-color: orange;    
  color: white;    
  padding: 10px;    
}     
</style>    
<body>    
<h2>Same Class with Different Tag</h2>    
<h2 class=\"fruit\">Mango</h2>    
<p class=\"fruit\">Mango is the king of all fruits.</p>    
</body>    
</html>  

HTML class类属性

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/6326.html

管理员

上一篇: HTML id 属性
下一篇: HTML使用CSS样式
相关推荐
2025-08-06

文章目录 一、Promise基础回顾 二、Promise 与 axios 结合使用场景及方法 (一)直接返回 axios …

269
2025-08-06

文章目录 一、模块初始化时的内部机制 二、常见导出写法的差异分析 (一)写法一:module.exports…

107
2025-08-06

文章目录 一、ResizeObserver详解 (一)ResizeObserver是什么 (二)ResizeObserver的基本用法 …

683
2025-08-06

文章目录 一、前期准备工作 (一)下载相关文件 (二)安装必要工具 二、处理扣子空间生成的文件…

338
2025-08-06

文章目录 一、官方文档 二、自动解包的数据类型 ref对象:无需.value即可访问 reactive对象:保持…

371
2025-08-06

文章目录 一、Hooks的工作原理 二、在if语句中使用Hook会出什么岔子? 三、React官方的Hook使用规…

843
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号