繁体   English   中英

使用onclick更改div内容

[英]Change div content with onclick

所以我已经被这个问题困扰了几个小时了……实际上已经差不多一天了。

请看看我为这个问题做了的jsfiddle

因此,我要在这里实现的是更改单击和选择箭头时显示的图像。 我希望他们改变成这个 选择时的指针

但这显然没有发生。 目前,背景图片正在显示,但显示在按钮的背面。 我想要的是显示一个新按钮/图像,该按钮将显示或覆盖旧按钮

这是js

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

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>

和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')
});

尝试这个

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

});

您不应更改div的背景,而应在单击图像时通过更改其来源来处理图像本身。 为您制作了新的提琴,以便您可以看到演示

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

为此,请给您的图像一个名为arrow的类-> class="arrow"

试试这个:- 演示

在CSS中替换

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

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

而这在jQuery中

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

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

您可以添加一些CSS以隐藏图像。

.selected img {
    opacity: 0;
}

分叉的JSFiddle

您需要做的是

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

演示

你怎么样交换图像的来源

  $('.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');

});

更新演示区

暂无
暂无

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

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