JavaScript-DOM编程的其他内容

操作DOM属性

var show=document.fetElementById(“show”);
1、面向对象的方式
show.title=“新的标题”;
show.stytle.color=“red”;
2、数组的方式
show[‘title’]="新的标题“;”
3、JavaScript提供的专门用来操作属性额API
获取:show.getAttribute(“title”);
改值:show.setAttribute(“title”,“新的值”);第一个参数是获取修改的值,第二个参数是修改内容

在这里插入图片描述
注意:操作类要用className这个属性

操作DOM对象的样式

var show=document.fetElementById(“show”);
DOM.style.样式只能拿到标签样式,css文件外联的拿不到
使用:getComputedStyle(div).width (IE9以下不支持) cuurentStyle(只低版本IE用)
在这里插入图片描述

抽奖案例

<!DOCTYPE html>
<html>
<head>
	<title>抽奖</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="./css/抽奖.css">
	
</head>
<body>
       <div class="box">
             <div id="goods" class="goods">奖品</div>
             <button id="start" onclick="ru()">开始抽奖</button>

       </div>
       <script type="text/javascript" src="./js/抽奖.js"></script>
</body>
</html>
.box{
	width: 400px;
	height: 400px;
	border:1px solid red;
	margin:auto;
	text-align: center;
}
.goods,.goods2{
	width: 200px;
	height: 200px;
	border-radius: 50%;
	text-align: center;
	font-size: 24px;
	font-weight: bold;
	color: white;
	line-height: 200px;
	margin:auto;
	background-color: red;
}
.goods2{
	background-color: green;
}

#start{
	width: 300px;
	height: 50px;
	margin:auto;
	text-align: center;
	line-height: 50px;
	font-size: 18px;
	color: black;
	background-color: #FEAB45;
	
	margin-top: 50px;
}

var start=document.getElementById('start');
var g=document.getElementById("goods");
var goods=["被子","手机","电脑","衣服","大保健","本子","书籍","再来一次","饮料"];
var flog=true;
let timer;

function ru(){
	
	if(flog){
		flog=false;
	timer=setInterval(function(){
		var index=Math.floor(Math.random()*goods.length);
		g.textContent=goods[index];
	    },10);
        start.innerText="停止抽奖";
        g.className="goods2";
    }else{
    	flog=true;
    	clearInterval(timer);
    	start.innerText="请开始抽奖";
         g.className="goods";
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值