html - handle jQuery hover behavior -


i've made simple example problem guess it's easier understand. have few divs have grey color, when hover on 1 of them, see true color. if click 1 of them (and alerts clicked) should change color , .hover() shouldn't work anymore on element, until 1 clicked. i'm sitting here 1 hour , don't work:

.test { background-color: #ccc;} <div class="test" id="d1"></div> <div class="test" id="d2"></div> <div class="test" id="d3"></div> 

and script:

$(function() { $('#d1').hover(function() { $(this).css('background-color', '#f00'); }, function() { $(this).css('background-color', '#ccc'); }); $('#d2').hover(function() { $(this).css('background-color', '#f0f'); }, function() { $(this).css('background-color', '#ccc'); }); $('#d3').hover(function() { $(this).css('background-color', '#00f'); }, function() { $(this).css('background-color', '#ccc'); });  $('#d1').click(function() { $(this).css('background-color','#f00'); alert("clicked")}); $('#d2').click(function() { $(this).css('background-color','#f0f'); alert("clicked")}); $('#d3').click(function() { $(this).css('background-color','#00f'); alert("clicked")}); 

})

for link click here

it seems hover still works , removes background color immediately.

thanks in advance!

well, without too many changes code [refactoring]:

$(function() {     var clickedid = '';      $('#d1').hover(function() {         $(this).css('background-color', '#f00');      }, function() {          if (this.id != clickedid) {             $(this).css('background-color', '#ccc');         }     });     $('#d2').hover(function() {          $(this).css('background-color', '#f0f');      }, function() {          if (this.id != clickedid) {             $(this).css('background-color', '#ccc');         }          });     $('#d3').hover(function() {          $(this).css('background-color', '#00f');     }, function() {         if (this.id != clickedid) {             $(this).css('background-color', '#ccc');         }     });      $('#d1').click(function() {         clickedid = this.id;         $('.test').not(this).css('background-color','#ccc');         alert("clicked");     });     $('#d2').click(function() {         clickedid = this.id;         $('.test').not(this).css('background-color','#ccc');         alert("clicked");     });     $('#d3').click(function() {         clickedid = this.id;         $('.test').not(this).css('background-color','#ccc');         alert("clicked");     });   }) 

changes:

  • use variable hold id of last clicked element
  • when click element, store id of clicked element. also, set elements (except 1 clicked) original background color.
  • on hover out, check if element losing hover id of last clicked element (if is, don't change background back).

as aside, use css classes , set .active clicked elements, , use .test:hover. assume rudimentary javascript example learning purposes.

and if wanted see 1 css: http://jsfiddle.net/mgtr4/1/


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -