admin管理员组文章数量:1395027
i have a question about changing the color of only Current element. So i want to change the background color of the current element by every click.
My problem is, i can not reset the background color of previous element.
For an example i have here two background color(yellow, lightblue), if i click on "old div" and "new div", than the background color of two divs changes to green. But only one div should be set to green.
So by every click must change only the background color of current element.
JavaScript
var divTag = document.getElementsByTagName("div");
for (var i = 0; i < divTag.length; i++) {
if (divTag[i].tagName == "DIV" || divTag[i].tagName == "div") {
if (divTag[i].addEventListener) {
divTag[i].addEventListener('click', callback,false);
}
else if (divTag[i].attachEvent) {
divTag[i].attachEvent('on' + 'click',callback);
}
}
}
function callback(e) {
e = e || window.event;
var target = e.target || e.srcElement;
target.style.backgroundColor = "green";
e.stopPropagation();
}
HTML
<div id="old">
<input style="margin: 10px;" type="textbox" />
<div id="new"></div>
</div>
CSS
#old {
width:200px;
height:200px;
background-color:yellow;
}
#new{
width:20px;
height:20px;
background-color:lightblue;
}
Can you help me? Thanks in advance
i have a question about changing the color of only Current element. So i want to change the background color of the current element by every click.
My problem is, i can not reset the background color of previous element.
For an example i have here two background color(yellow, lightblue), if i click on "old div" and "new div", than the background color of two divs changes to green. But only one div should be set to green.
So by every click must change only the background color of current element.
JavaScript
var divTag = document.getElementsByTagName("div");
for (var i = 0; i < divTag.length; i++) {
if (divTag[i].tagName == "DIV" || divTag[i].tagName == "div") {
if (divTag[i].addEventListener) {
divTag[i].addEventListener('click', callback,false);
}
else if (divTag[i].attachEvent) {
divTag[i].attachEvent('on' + 'click',callback);
}
}
}
function callback(e) {
e = e || window.event;
var target = e.target || e.srcElement;
target.style.backgroundColor = "green";
e.stopPropagation();
}
HTML
<div id="old">
<input style="margin: 10px;" type="textbox" />
<div id="new"></div>
</div>
CSS
#old {
width:200px;
height:200px;
background-color:yellow;
}
#new{
width:20px;
height:20px;
background-color:lightblue;
}
Can you help me? Thanks in advance
Share Improve this question edited Nov 20, 2014 at 17:11 GoBusto 4,7886 gold badges31 silver badges46 bronze badges asked Nov 20, 2014 at 16:42 aldimeola1122aldimeola1122 8185 gold badges14 silver badges24 bronze badges2 Answers
Reset to default 3If you worried about performance, you can just save previous and reset it's background color to normal.
var prevDiv = null;
function callback(e) {
e = e || window.event;
var target = e.target || e.srcElement;
if(prevDiv) {
prevDiv.style.backgroundColor = '';
}
target.style.backgroundColor = 'green';
prevDiv = target;
e.stopPropagation();
}
http://jsfiddle/v6d6veyv/
Reset the background colour of all other divs when you click one.
var divs = document.getElementsByTagName("div");
for(var i = 0; i < divs.length; i++){
divs[i].style.backgroundColor = '';
}
http://jsfiddle/4uosvc75/
本文标签: htmlJavaScript change the color of current element by clckStack Overflow
版权声明:本文标题:html - JavaScript change the color of current element by clck - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107304a2591125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论