码迷,mamicode.com
首页 > Web开发 > 详细

js小练习——在页面中点击哪里,哪里出现div(圆)

时间:2017-04-23 22:29:11      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:css   radius   ffffff   head   and   setattr   节点   设置   int   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点哪,哪出15*15的圆型div</title>
<style type="text/css">
html,body{
height: 100%;
margin: 0;
padding: 0;
}
.yuan {
width: 16px;
height: 16px;
border-radius: 50%;
position: absolute;
}
</style>
</head>
<body>
<script>
/**
* 点哪 哪出15*15的圆型div
*/
document.body.addEventListener(‘click‘, function (e) {
var div = document.createElement(‘div‘); //创建div元素
var className = document.createAttribute(‘class‘);//创建class属性
className.value = ‘yuan‘;//属性赋值
div.style.left = e.clientX - 16/2 + ‘px‘;
div.style.top = e.clientY - 16/2 + ‘px‘;
div.setAttributeNode(className);//设置属性节点
div.style.backgroundColor = ‘#‘ + parseInt(0xffffff * Math.random()).toString(16);//随机背景颜色
document.body.appendChild(div);//给body添加元素div
})

</script>
</body>
</html>

js小练习——在页面中点击哪里,哪里出现div(圆)

标签:css   radius   ffffff   head   and   setattr   节点   设置   int   

原文地址:http://www.cnblogs.com/chuangzi/p/6754135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!