简体   繁体   中英

Change div content with onclick

So i've been stuck with this problem for hours now... actually almost a day already.

Please look at the jsfiddle I did for this problem

So what I'm trying to achieve here is to change the image being shown when the arrows are clicked and selected. I wanted them to change into this 选择时的指针

but it's obviously not happening. Right now, the background image is showing but it's appearing at the back of the button. What I want is to show a new button/image that would appear or would cover the old button

Here's the js

$('.step_wrapper').on('click','.step_box',function () {
$('.step_box').removeClass('selected');
$(this).addClass('selected')
});

The HTML

  <div style="float: left;">
   <div class="step_wrapper">
    <div class="step_box" onclick="show('Page2');">  
   <a href="#"><span class="RectW"><img src="http://i59.tinypic.com/2lcvjlz.png" width="25" height="25" ></span></a> 
  </div>
    </div></div>


  <div style="float: left;">
   <div class="step_wrapper">
    <div class="step_box" onclick="show('Page2');">  
   <a href="#"><span class="RectW"><img src="http://i59.tinypic.com/2lcvjlz.png" width="25" height="25" ></span></a> 
    </div>
    </div></div>

and the CSS, you can look at the fiddle :)

 <div style="float: left; width:400px;">
 <div class="step_wrapper">
 <a class="step_box" onclick="show('Page2');"><img src="http://i59.tinypic.com/2lcvjlz.png" width="25" height="25" ></a> 
</div>

 <div class="step_wrapper">

 <a class="step_box"><img src="http://i59.tinypic.com/2lcvjlz.png" width="25" height="25" ></span></a> 

  </div></div>


.step_wrapper{
    display:inline;
    width: 25px;
    height: 25px;
}

.step_box {

}

.selected{
    /*background: black;*/
}


.PointWHover{
    display: none;
}

.PointWHover  .notch {
    position: absolute;
top: -7px;
left: 2px;
margin: 0;
border-top: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #676767;
padding: 0;
width: 0;
height: 0;
font-size: 0;
line-height: 0;
_border-right-color: pink;
_border-left-color: pink;
_filter: chroma(color=pink);
}

a:hover + .PointWHover {
    display: inline;
background: none repeat scroll 0% 0% #676767;
padding: 5px;
top: 35px;
left: 4px;
color: #FFF;
position: absolute;
font-family: Arial;
font-size: 12px;
font-weight: bold;
}




  $('.step_wrapper').on('click','.step_box',function () {

    var url = $(this).find( "img" ).attr( "src","http://ij-plugins.sourceforge.net/vtk-examples/SimpleCone.jpg");
      alert(url);

    $('.step_box').removeClass('selected');
    $(this).addClass('selected')
});

Try this

$('.step_wrapper').on('click','.step_box',function () {
    //your code here
    $(this).find("img").attr("src","your url here");

});

You should not change the background of the div, but you should handle your image itself by changing his source when clicking on it. Made a new fiddle for you so you can see a DEMO

 $('.arrow').on('click', function () {
      $('.arrow').attr('src','http://i59.tinypic.com/2lcvjlz.png');
      $(this).attr('src', 'http://i62.tinypic.com/2vb8kn8.png')
  });

To do this, give your image a class named arrow --> class="arrow"

Try this:- DEMO

Replace this in CSS

.selected{
    background: url("http://i62.tinypic.com/2vb8kn8.png");
    z-index:99999;
}

With

.selected{
    background: url("http://i62.tinypic.com/2vb8kn8.png");
    z-index:99999;
    height:25px;
    width:25px'
}

And this in jQuery

$('.step_wrapper').on('click','.step_box',function () {
    $('.step_box').removeClass('selected');
    $(this).addClass('selected')
});

With

$('.step_wrapper').on('click','.step_box',function () {
    $('.step_box').removeClass('selected');
    $('.step_box img').fadeIn('fast');
    $(this).addClass('selected');
    $(this).find('img').fadeOut('fast');
});

You could add a little css to hide the image.

.selected img {
    opacity: 0;
}

forked JSFiddle

What you need to do is

$('.step_wrapper').on('click','.step_box',function () {
$('.step_box').removeClass('selected');
  $(this).find("img").animate({opacity : 0},10)
$(this).addClass('selected')
});

DEMO

How about you just swap the source of the image

  $('.step_wrapper').on('click','.step_box',function () {

  $('.step_box img').attr('src','http://i59.tinypic.com/2lcvjlz.png');
      $(this).find('img').attr('src','http://i.stack.imgur.com/0e6C3.png');

});

UPDATED DEMO FIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM