简体   繁体   English

点击后如何设置它包括JQUERY中的图像和div跨度内容

[英]How to set after onclick it include image and div span contents in JQUERY

I have a code using CSS sprite , as the code is working although i clicking on the image and the whole div contents change color include the contents word . 我有一个使用CSS精灵的代码,因为代码工作,虽然我点击图像和整个div内容更改颜色包括内容字。 But when i clicking on the contents word which stacked with images indeed the contents word does the changes of the color but the images still remain as the same without changing to blue. 但是当我点击堆叠有图像的内容词时,内容词确实改变了颜色,但图像仍然保持不变而不改变为蓝色。

Can we do it in Jquery method to make this function work ? 我们可以用Jquery方法来实现这个功能吗?

 /*Hide the Radio Button*/ .games-sub-menu input[type=radio] { display: none } /*Set a box for the label, this is what is clicked on*/ .games-sub-menu label { display: block; width: 150px; height: 100px; } /*Set Images...this would work better with sprites*/ .games-sub-menu label.topimgG1 { background-image: url("http://placehold.it/150x100/FF0000/FFFFFF/?text=Image1"); } .games-sub-menu input[type=radio]:checked + label.topimgG1 { background-image: url("http://placehold.it/150x100/?text=Image1"); } .games-sub-menu label.topimgG2 { background-image: url("http://placehold.it/150x100/00FF00/FFFFFF/?text=Image2"); } .games-sub-menu input[type=radio]:checked + label.topimgG2 { background-image: url("http://placehold.it/150x100/?text=Image2"); } 
 <div class="games-platform-item pt-item"> <ul class="games-sub-menu clearfix"> <li class="tab1 current"> <input type="radio" name="imgSwap" id="rdoImg1"> <label class="topimgG1" for="rdoImg1"></label> <span>编辑精选</span> </li> <li class="tab2 current"> <input type="radio" name="imgSwap" id="rdoImg2"> <label class="topimgG2" for="rdoImg2"></label> <span>编辑精选</span> </li> </ul> </div> 

Image of the div : div的形象:

在此输入图像描述

Change your html code that way, that the span which holds the text will be the child element of the label, that way, clik on the text will also be click on the label which will change the check state of the input, and your css will change too: 以这种方式更改你的html代码,保存文本的span将是标签的子元素,这样,文本上的clik也会点击标签,这将改变输入的检查状态,以及你的css也将改变:

<div class="games-platform-item pt-item">
  <ul class="games-sub-menu clearfix">
    <li class="tab2 current">
      <label class="topimgG2" for="rdoImg2">
         <input type="radio" name="imgSwap" id="rdoImg2">
         <span>编辑精选</span>      
      </label>
    </li>
  </ul>
</div>

Here is an example, I changed also your css a little bit: 这是一个例子,我也改变了你的css:

 .games-sub-menu input[type=radio] { display: none } .games-sub-menu label { display: block; width: 150px; height: 100px; margin-bottom: 2em; } .games-sub-menu label.topimgG1 span::before { display:block; width: 100%; content: "\\00a0"; height: 100%; background-image: url("http://placehold.it/150x100/FF0000/FFFFFF/?text=Image1"); } .games-sub-menu label.topimgG2 span::before { display:block; width: 100%; content: "\\00a0"; height: 100%; background-image: url("http://placehold.it/150x100/00FF00/FFFFFF/?text=Image2"); } .games-sub-menu label.topimgG1 input[type=radio]:checked + span::before { background-image: url("http://placehold.it/150x100/?text=Image1"); } .games-sub-menu label.topimgG2 input[type=radio]:checked + span::before { background-image: url("http://placehold.it/150x100/?text=Image2"); } 
 <div class="games-platform-item pt-item"> <ul class="games-sub-menu clearfix"> <li class="tab1 current"> <label class="topimgG1" for="rdoImg1"> <input type="radio" name="imgSwap" id="rdoImg1"> <span>编辑精选</span> </label> </li> <li class="tab2 current"> <label class="topimgG2" for="rdoImg2"> <input type="radio" name="imgSwap" id="rdoImg2"> <span>编辑精选</span> </label> </li> </ul> </div> 

On click event just change the background image of that particular div whereever you are clicking. click事件上,只需更改您单击的特定div的背景图像。

By jQuery you can remove previous class and add another class which has different background property. 的jQuery你可以删除以前的class并添加另一个class具有不同的背景属性。

Example - 示例 -

.games-sub-menu label.topimgG1Clicked {
  background-image: url("http://placehold.it/150x100/FF0000/FFFFFF/?text=ImageChangedImage");
}

In javascript click event: 在javascript click事件:

$(this).closest('label').removeClass('topimgG1');
$(this).closest('label').removeClass('topimgG1Clicked');

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

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