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"。
效果:

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