码迷,mamicode.com
首页 > 其他好文 > 详细

DOM

时间:2016-07-21 00:24:48      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1、搜索框默认值为“请输入账户",当获取焦点时清空文本内容,失去焦点时,若文本内容为空或空格,则将文档本人重置为”请输入账户"。

<script type="text/javascript">
	function Focus(){
		var a = document.getElementById("account1");
		if(a.value == "请输入账户"){
			a.value = "";
		}
	}
	function Blur(){
		var a = document.getElementById("account1");
		if(a.value.trim()==""){
			a.value="请输入账户";
		}
	}
</script>
</head>
<body>
<input id="account1" type="text" value="请输入账户" name="account" onfocus="Focus();" onblur="Blur()";/>
</body>

2、添加标签。

<div id="creatediv1" style="width:50px;height: 50px;background-color: red;></div>
<input type="button" value="添加" onclick="creatediv2();" />
<script type="text/javascript">
	function creatediv1(){
		var cobj = document.createElement("a");
		cobj.href = "http://www.baidu.com";
		cobj.innerText = "百度";
		var nid = document.getElementById("creatediv1");
		nid.appendChild(cobj)
	}
	
	function creatediv2(){
		var nid = document.getElementById("creatediv1");
		var tag = "<a href=‘http://www.baidu.com‘>百度</a>";
                //beforeBein在div之前,beforeEnd在div内部,afterBegin在div内部,afterEnd在div后面
		nid.insertAdjacentHTML("beforeBegin",tag);
	}
</script>

3、设置获取标签属性

var n = document.getElementById("creatediv1");
n.setAttribute("sb","ooxx");
console.log(n);
b = n.getAttribute("sb");
console.log(b);

特殊:n.className,n,style.fontSize="100px"。

效果:

技术分享

 

DOM

标签:

原文地址:http://www.cnblogs.com/owasp/p/5690125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!