HTML5 新增的属性,你用过几个?

549 阅读3分钟

HTML5保留了以前HTML的绝大部分元素,也新增了一部分通用属性,我们来一起看一下

1.contenEditable属性

HTML5为大部分HTML元素增加了contenEditable属性,如果将该属性设为true,就可以直接编辑该HTML元素的内容(不包括本身就允许输入的表单元素),并且contenEditable具有可继承的特点:如果一个元素的父元素contenEditable为true,那么它默认该子元素也是可编辑的,除非指定为false

使用方法:

<html>
	<head>
    	<meta charset="utf-8">
    	<title></title>
	</head>
	<style>
		table tr,table td{width:50%;}
	</style>
	<body>
		<table  contenteditable="true" border="1" style="width: 400px; border-collapse: collapse;">
		    <tr>
		        <td>html</td>
		        <td>html5</td>
		    </tr>
		    <tr>
		        <td>css</td>
		        <td>css3</td>
		    </tr>
		</table>
	</body>
</html>

显示效果:

2.designMode属性

designMode属性相当于一个全局的contenEditable属性,如果把整个页面的designMode属性设置为on,则该页面上所有可以支持contenEditable属性的元素都会变成可编辑状态,其默认属性为off

注:designMode只能在javascript脚本里面被编辑修改

使用方法:

<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h2>我是h2</h2>
		<ul>
			<li>这是街舞</li>
			<li>这是篮球</li>
		</ul>
		<script type="text/javascript">
		   document.designMode = "on";
		</script>
	</body>
</html>

显示效果:

3.spellcheck属性

HTML5为<input.../><texteara.../>等元素增加了spellcheck属性,他是支持boolean的属性,如果设置了该属性,浏览器会对输入的文本内容进行检查,如检查未通过,浏览器会对拼错的单词进行提示。

使用方法:

<body>
    <p contenteditable="true" spellcheck="true">这是可编辑的段落:</p>
    <input type="text" name="fname" spellcheck="true">
</body>

显示效果:

4.contextmenu属性

用以表示元素的上下文菜单,当用户右击元素时将显示上下文菜单contextmenu 属性的值是需要打开的 <menu> 元素的 id

使用方法:

	<body>
		<div oncontextmenu="myfunction()" contextmenu="menu">
			<div style='background:#FFC107;width: 80px;text-align:center;border: 1px solid #FFC107;'>点击我</div>
			<menu id="menu" type='context'>
				<!--menuitem只在 Firefox 中有效!-->
				<menuitem label="Refresh" onclick="window.location.reload();" icon="ico_reload.png"></menuitem>
					<menu label="Share on...">
						<menuitem label="Twitter" icon="ico_twitter.png" onclick="window.open('//twitter.com/intent/tweet?text=' + window.location.href);"></menuitem>
						<menuitem label="Facebook" icon="ico_facebook.png" onclick="window.open('//facebook.com/sharer/sharer.php?u=' + window.location.href);"></menuitem>
					</menu>
				<menuitem label="Email This Page" onclick="window.location='mailto:?body='+window.location.href;"></menuitem>
			</menu>
		</div>
		<p id="demo"></p>
		<script type="text/javascript">
			function myfunction(){
				let x = document.getElementById("demo");
				x.innerHTML = "你在 div 中点击了鼠标右键!";
			}
			/**阻止浏览器默认右键点击事件*/
			$(document).bind('contextmenu',function (){
				console.log('点击鼠标右键:'+ new Date().getTime())
				return false;
			})
		</script>
	</body>

显示效果:

当然我们也在火狐浏览器从中试了一下menuitem元素的内容,显示的菜单如下