节点操作案例

改变字体颜色和大小

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);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值