jquery - Change image upon click, revert on click anywhere -


I am trying to create a script which:

the image changes when the image is clicked ,

br> Then return back to the original image, whenever clicked anywhere on the screen.

Currently, my script only changes when the exact image is clicked.

Changes to Image Clicks:

  & lt ;! - Cardstats - & gt; & Lt; Img src = "toggle-on page" class = "img-swap" />   

Click the jQuery script to change the image:

  / * ToggleOn & amp; Toggleoff * / $ (function () {$ (".img-swap"). Live ("click", function () {if ($ (this) .attr ("class") == "img-swap" } {This.src = this.src.replace ("toggle-on", "toggle-off");} and {this.src = this.src.replace ("toggle-off", "toggle-on") ;} $ (This) .toggleClass ("toggle-on");});});     

Here is an updated code that can be used by .bind ( ) as suggested by Joel , as it is right, its best support if the image is clicked, then it will swap the image, if it is clicked again If it is done, it will change back in the form of clicking anywhere in the body OK . Due to the dynamic nature of the host, I am due to the full url. Yoga had to, but your original replacement code should work fine.

Instead of running two tasks, we shrink it in one. It can not work with other .click () events, without adding event handler along with other events. As I did in this function and .click () events

  var img = $ ('. Img-swap'), IMG = new array ('http : //s27.postimg.org/i6knc8y0j/img1.jpg ',' http: / /s12.postimg.org/k68so1del/img2.jpg '); // Close is the first image isOn = false; $ (Document) .bind ("click", function (e) {if ($ (e.target) .attr ('class') == img.attr ('class')) {// Do we click on it Image (ifon == incorrect) {img.attr ('src', imgs [1]); isOn = true;} Other {img.attr ('src', imgs [0]); isOn = false; }} Else {// No, document or something else (iin) {img.attr ('src', IMGS [0]); isOn = false;}}}} should be;   

The following method tries to use the dynamic element. This is multiple cards flipped at a time, and this will not work when closing the last clicked card only.

  var lastClick = undefined; $ (Document) .bund ("click", function (e) {if ($ (e.target) .attr ('class') == 'img-swap') {var targetimg = $ (e.target) ; ('Toggle-on')! == -1) {targetimg.attr ('src', targetimg.attr (as 'src')). ('Toggle-on', 'toggle-off')); else if (targetimg.attr ('src')) ('toggle-off')! == -1) {targetimg.attr () ' Src ', targetimg.attr (' src '). ('Toggle-off', 'toggle-on'));}} else {if (lastClick & lastClick.attr ('src'). ('Toggle-on')! == -1) { LastClick.attr ('src', lastClick.attr ('src'). Change ('toggle-on', 'toggle-off')); Last click = undefined;}}});   

So you are flipping more than one card, then all img-swap at a time > You may need to turn off like this example.

  var imageTags = $ ('. Img-swap'), lastClick = undefined; $ (Document) .bund ("click", function (e) {if ($ (e.target) .attr ('class') == 'img-swap') {var targetimg = $ (e.target) ; ('Toggle-on')! == -1) {targetimg.attr ('src', targetimg.attr (as 'src')). ('Toggle-on', 'toggle-off')); else if (targetimg.attr ('src')) ('toggle-off')! == -1) {targetimg.attr () ' Src ', targetimg.attr (' src '). ('Toggle-off', 'toggle-on'));}} {imageTags.each (function () {if ($ (this) .attr (('toggle on') == -1) {$ (this Replace ('Toggle-On', 'Toggle-Off')))}}}}}}});   



Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -