单个div上的多个onclick事件 - 更改div边框颜色 - javascript [英] multiple onclick events on a single div - changing the div border colors - javascript

查看:865
本文介绍了单个div上的多个onclick事件 - 更改div边框颜色 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把这段代码放在div中:

  onclick =this.style.border ='solid 1px red ; 

如何插入多个触发边界颜色变化的onclick事件 - 例如第一次点击 - 红色,第二次点击 - 蓝色,第三次点击 - 重置?



这正是我需要的 - 使用多个onclick事件更改一个特定div,而不是多个div的即兴创建。 / p>

编辑:



我还有其他的操作,使用onclick +我有onmouseenter和onmouseleave事件DIV。一切工作,因为它应该,我只能不能让多个onclick事件工作。



我不能得到任何这些功能的工作,我不能告诉为什么。也许是因为我有更多的动作和事件,或者也许是Wordpress?



(我在<脚本>< / script>



我认为这会比播放声音容易一些,但令人惊讶的是,这似乎是一个真正的挑战......

有人可以帮忙吗?

edit2:这取决于Wordpres。现在我知道如何制作适当的功能。



edit3 :我编辑了user1875968发布的一个小解决方案, (正确重置):

  var linkClick = 1; 
function update_link(obj){
if(linkClick == 1){obj.style.border ='solid 1px red'};
if(linkClick == 2){obj.style.border ='solid 1px blue'};
if(linkClick == 3){obj.style.border ='solid 1px green'};
if(linkClick> 3){obj.style.border ='solid 1px#555555'; linkClick = 0};
linkClick ++;
}

感谢大家的帮助;)

解决方案

 < script> 
var linkClick = 0;
函数update_link(obj){
if(linkClick == 0){obj.style.border ='solid 1px red'};
if(linkClick == 1){obj.style.border ='solid 1px blue'};
if(linkClick> 1){obj.style.border ='solid 1px red'; linkClick = 0};
linkClick ++;
}
< / script>

HTML

 < a href ='#'onclick ='update_link(this)'>我的链接< / a> 


I've put this code inside the div:

onclick="this.style.border='solid 1px red';"

How to insert multiple onclick events which would trigger changes of border colors - for example 1st click - red, 2nd click - blue, 3rd click - reset?

This is exactly what I need - changes of the one particular div with multiple onclick events, not improvisations with multiple divs.

edit:

I also have other actions called with onclick + I have onmouseenter and onmouseleave events inside of the same div. Everything work as it should, I only can't get multiple onclick events to work.

I can't get any of these functions to work and I can't tell why. Maybe because I have more actions and events, or maybe it's up to Wordpress?

(I put a function in the header area inside of <script></script>)

I thought this will be easier than playing a sound, but surprisingly sounds were a piece of cake and this seems to be a real challenge...

can somebody help?

edit2: It was up to Wordpres. Now I know how to make proper functions to work.

edit3: I've edited a little solution posted by user1875968, and I finally got exactly what I want (with proper reset):

var linkClick = 1; 
  function update_link(obj){ 
  if (linkClick == 1){obj.style.border = 'solid 1px red'};
  if (linkClick == 2){obj.style.border = 'solid 1px blue'};
  if (linkClick == 3){obj.style.border = 'solid 1px green'};
  if (linkClick >3 ) {obj.style.border = 'solid 1px #555555'; linkClick=0};
  linkClick++; 
}

thanks everybody for your help ;)

解决方案

<script> 
var linkClick = 0; 
  function update_link(obj){ 
  if (linkClick == 0){obj.style.border = 'solid 1px red'};
  if (linkClick == 1){obj.style.border = 'solid 1px blue'};
  if (linkClick >1 ) {obj.style.border = 'solid 1px red'; linkClick=0};
  linkClick++; 
}
</script>

HTML

<a href='#' onclick='update_link(this)'> my link </a>

这篇关于单个div上的多个onclick事件 - 更改div边框颜色 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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