Javascript:如何获得div的随机余量? [英] Javascript: how to get random margin for div?

查看:168
本文介绍了Javascript:如何获得div的随机余量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我体内有div.当我单击该div时,它必须获得随机的边距并更改其位置.我该怎么办?

我尝试过的事情:

我已经这样尝试过了

I have div in the body. When i click on that div, it must get random margin and change its position. How can i do that?

What I have tried:

I have tried like this`

.div1{
			height: 200px;
			width: 200px;
			background-color: red;
		}

<div class="div1"></div>


var arr = document.querySelectorAll(".div1");
for(var i = 0; i < arr.length; i++){
	arr[i].addEventListener("click", F);
}

function F(){
 	var margin = parseInt(Math.random()*500);
 	this.style.margin = margin;
 }

推荐答案

我用Google搜索了设置样式边距",这是第一个结果

HTML DOM样式边距属性 [
I googled "set style margin" and this was the first result

HTML DOM Style margin Property[^]

use that to show you how to properly set that property.


这篇关于Javascript:如何获得div的随机余量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆