HTML的一些属性和全局属性

本文深入讲解HTML全局属性,包括id、class、title等基础属性,以及HTML5新增的contenteditable、data-*、hidden等特性。通过实例演示如何使用这些属性增强网页功能。

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

HTML属性

内容提纲

  1. html属性
  2. html全局属性

[html属性]

属性为 HTML 元素提供附加信息。

属性一般以名称/值对的形式出现,比如:<img src="abc.jpg" alt="替换文本">

属性也可以没有属性值,比如:<input type="text" value="无值属性用法" disabled>

html全局属性

所有html标签有效的属性,w3school w3

[slide]

html全局属性

属性描述
id规定元素的唯一id
class规定元素的一个或多个类名,引用样式表中的类
title规定有关元素的额外信息,鼠标悬停提示
accesskey规定激活元素的快捷键,Alt+指定键
tabindex规定元素的tab键次序
style规定元素的行内 CSS 样式
lang规定元素内容的语言

html全局属性示例

<a 
  id="lizhiyuan" 
  class="link" 
  title="访问我的地址" 
  accesskey="z"
  tabindex="2" 
  href="//github.com/aylizhiyuan" 
  target="_blank"
  style="text-shadow: 1px 1px 1px #000">Mr.zy</a> 

Mr.zy

html全局属性-html5新增

属性描述
contenteditable规定元素内容是否可编辑
data-*用于存储页面或应用程序的私有定制数据
hidden规定元素是否隐藏
spellcheck规定是否对元素进行拼写和语法检查
translate规定是否应该翻译元素内容

html5新增全局属性示例

<h2 contenteditable>可编辑标题</h2>
<h2 hidden>编辑标题之后显示</h2>

可编辑标题

编辑标题之后显示

data-*属性-自定义存放数据属性

<div class="spaceship" 
	data-ship-id="92432"
    data-weapons="laser 2" 
    data-shields="50%"
    data-x="30" 
    data-y="10" 
    data-z="90">
	<button class="fire"
		οnclick="alert("第"+this.parentNode.dataset.shipId+"号飞船开始发射!")">
		Fire
	</button>
</div>
Fire

translate属性-标签内容是否翻译

<p>The Bee Game is a text adventure game in English. </p>
<p>When the game launches, the first thing you should do 
is type <kbd translate="no">eat honey</kbd>.</p> 
<p>The game will respond with:<samp translate="no">Yum yum! That was some good honey!</samp></p>

The Bee Game is a text adventure game in English.

When the game launches, the first thing you should do is type eat honey.

The game will respond with:Yum yum! That was some good honey!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值