如何用vuejs,点击div改变div的颜色

yifanict 2017-03-10 03:10:19
在组件的中应该怎么写?
...全文
3985 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
MJ.XU 2017-07-14
  • 打赏
  • 举报
回复
scscms太阳光 2017-03-13
  • 打赏
  • 举报
回复
<div id="app"> <div @click="fun($event)">sdfsdf</div> </div> <script> new Vue({ el: '#app', methods: { fun(e){ e.target.style.backgroundColor = "#"+Math.floor(Math.random()*0xffffff).toString(16); } } }); </script>
浪子回头8 2017-03-13
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<div onclick='chgColor(this)'>1</div>
<div onclick='chgColor(this)'>2</div>
<div onclick='chgColor(this)'>3</div>
<script type="text/javascript">
var perDiv=null;
function chgColor(_this)
{
  if(perDiv) perDiv.style.backgroundColor='';
  _this.style.backgroundColor='red';
  perDiv=_this;
}
</script>
</body>
</html>

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧