DOM零碎知识点归纳

本人录制技术视频地址:https://edu.csdn.net/lecturer/1899 欢迎观看。

一、各种浏览器的支持性 

1. window.navigate(url);//将网页重新导航到url,只支持IE、Opera,建议使用window.location.href=‘url’;//几大浏览器都支持!

2. οnbefοreunlοad="window.event.returnValue='你确定离开吗?'";好像只有IE支持! 直接使用οnbefοreunlοad="return '你确定离开吗?'";大多数浏览器都支持!

3. <a href="http://www.baidu/com" οnclick="window.event.returnValue=false;">去百度</a>取消默认事件window.event.returnValue=false; IE和FF支持,其他的不支持
还是用return false;几乎都支持。

4. 关于innerText与innerHTML的支持问题对于innerHTML所有浏览器都支持,而innerText几乎所有浏览器都支持,但FF不支持! 

二、setInterval,clearInterval,setTimeout,clearTimeout的用法  

1. 每隔一秒钟,文本框的内容自增1。

效果图:


<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <title>dadadfa</title>
       <script type="text/javascript">
			var intervalId = setInterval(function () {
				var num = document.getElementById('txt').value; 
				num = parseInt(num) + 1; 
				document.getElementById('txt').value = num;
				}, 1000);

			function stopIt() { 
				clearInterval(intervalId);
			} 
		</script>
	</head> 
	<body>
		<input type="text" name="name" id="txt" value="1" />
		<input type="button" name="name" value="Stop" οnclick="stopIt();" /> 
	</body>
</html>

因为是在1秒钟之后,才执行setInterval方法,那个时候文本框已经加载完毕,所以不会报错! 

2. 流水灯效果。

效果图:


<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<title>12345</title> 
	<script type="text/javascript">
		var direction = 'left'; //定义滚动的方向 
		var intervalId = setInterval(function () {
			var title = document.title; 
			if (direction == 'left') {
				var front = title.charAt(0); 
				var rear = title.substr(1); 
				document.title = rear + front;
			}
			else if (direction == 'right') {
				var front = title.substr(0, title.length - 1); 
				var rear = title.substr(title.length - 1); 
				document.title = rear + front;
			}
		}, 500);
		function goLeft() { 
			direction = 'left';
		}
		function goRight() { 
			direction = 'right';
		} 
	</script>
</head> 
<body>
	Flow
	<input type="button" name="name" value="left" οnclick="goLeft();" /> 
	<input type="button" name="name" value="right" οnclick="goRight();" />
</body>
</html>

三、 onload、onunload、onbeforeunload  

onload 页面加载后触发 一般用window.onload代替使用

onunload 页面卸载后触发
onbeforeunload 页面卸载前触发 最好使用,οnbefοreunlοad="return '提示信息'"。

四、取消默认事件 

1.   <a href="http://www.baidu/com" οnclick="alert('Good');return false;">Baidu</a>

点击文字后,会弹出 "Good", 但是不会跳转到百度。

2.   用js的方式实现

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
    	<script type="text/javascript">
			function Judge() { 
				alert('Fobbiden'); 
				return false;
			} 
		</script>
</head>
<body>
	<a href="http://www.baidu.com" οnclick="Judge();">Baidu</a> 
</body>
</html>
注意,Judge函数本身的返回值为false,并不是onclick这个方法本身返回false,所以上述的代码执行后,依旧会跳转到百度,正确的写法应该是 onclick = "return Judge();"。

五、clipboardData对象 

setData('text',val); 设置剪贴板中的值

getData('text'); 读取剪切板的值

clearData('text'); 清空剪贴板中的内容

onpaste,oncopy 粘贴,拷贝时执行的操作

Demo效果图:



<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <title></title>
       <script type="text/javascript">
			function toCopy() {
				var comment = document.getElementById('comment').value; 
				comment = comment + '\r\n' + window.location.href; 
				//将复制内容加上网站地址加到剪贴板中 
				clipboardData.setData('text',comment);
			} 
		</script>
	</head> 
<body>
	try to copy:<input type="text" name="name" value="" οncοpy="alert('fobbiden to copy');return false;" /><br />
	try to paste:<input type="text" name="name" value="" οnpaste="alert('fobbiden to paste');return false;"/><br />
	<textarea cols="40" rows="7" id="comment">contentcontentcontentcontentcontentcontent contentcontentcontentcontentcontentcontent contentcontentcontentcontentcontentcontent contentcontentcontentcontentcontentcontent
	</textarea>
	<input type="button" name="name" value="copy the content of comments" οnclick="toCopy();"/>
</body>
</html>

六、 window、document、body的作用 

window是顶级对象,相当于C#中的object
document是浏览器整个浏览区域
body是指,浏览器区域有内容的范围因此要获得onclick事件,最好使用document.onclick,而不是body.onclick 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋恨雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值