改变字体颜色和大小
html+css
<body>
<div id="div1">
节点操作案例
</div>
</body>
#div1{width: 400px;height: 200px;border: 1px black solid;margin: 40px auto;text-align: center;font-size: 18px;line-height: 200px;}
随机颜色的函数
function randomColor(){
var str="rgba("+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+","+parseInt(Math.random()*256)+")";
return str;
}
实现字体颜色大小周期改变的动画效果
var r=1;
var i=0;
window.onload=function(){
var oDiv=document.getElementById("div1");
setInterval(function(){
oDiv.style.color=randomColor();
var fs=getComputedStyle(oDiv)["fontSize"];
oDiv.style.fontSize=parseInt(fs)+r+"px";
i++;
if(i%6==0){
r=-r;
}
},1000);
}
节点操作
html和css代码
<body>
<div id="div1">
<input type="text" name="" id="input1" placeholder="输入内容" />
<button type="button">增加</button>
<button type="button">删除</button>
<button type="button">拷贝</button>
</div>
<div id="div2">
</div>
</body>
<style type="text/css">
#div1{width: 400px;height: 100px;background-color: #FFA500;margin: auto;text-align: center;line-height: 50px;}
#div2{width: 400px;height: 500px;background-color: peachpuff;margin: auto;}
#input1{width: 380px;height: 30px;font-size: 18px;}
#div2 div{border-bottom: 1px dashed gray;padding: 2px;position: relative;}
#div2 button{position: absolute;right:0px;}
</style>
JS代码
window.onload=function(){
var oDiv1=document.getElementById("div1");
var oDiv2=document.getElementById("div2");
var oInput=document.getElementById("input1")
var aBtns=oDiv1.getElementsByTagName("button");
aBtns[0].onclick=function(){
if(!oInput.value){
alert("输入内容不能为空")
}
else{
var newDiv=document.createElement("div");
var oTxt=document.createTextNode(oInput.value);
newDiv.appendChild(oTxt);
oDiv2.appendChild(newDiv);
}
}
aBtns[1].onclick=function(){
oDiv2.removeChild(oDiv2.lastChild);
}
aBtns[2].onclick=function(){
var newNode=oDiv2.lastChild.cloneNode(true);
oDiv2.appendChild(newNode);
}
}