简体   繁体   English

使用onclick更改div内容

[英]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 请看看我为这个问题做了的jsfiddle

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 这是js

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

The HTML 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 :) 和CSS,您可以看一下小提琴:)

 <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. 您不应更改div的背景,而应在单击图像时通过更改其来源来处理图像本身。 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" 为此,请给您的图像一个名为arrow的类-> class="arrow"

Try this:- DEMO 试试这个:- 演示

Replace this in CSS 在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 而这在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. 您可以添加一些CSS以隐藏图像。

.selected img {
    opacity: 0;
}

forked JSFiddle 分叉的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 更新演示区

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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