如何在javascript中同时选择2张图片?

时间:2017-02-06 19:41:03

标签: javascript jquery html

您好我正在尝试学习如何选择图片,到目前为止我已经完成了这项工作。我只是不知道如何同时选择2张图片,因为我已经尝试删除images_list li函数中的.removeClass('selected');

HTML:

<div class="images_list">
  <li class="border" title="content_1">
      <img src="http://www.p69.com.br/wp-content/uploads/2013/04/imagens-lindas-6.jpg?0bce15" width="150" height="150" />
      <span>
          <img src="http://icons.iconarchive.com/icons/icojam/blue-bits/24/symbol-check-icon.png" />
      </span>
    </li>
  <li class="border" title="content_2">
      <img src="http://www.p69.com.br/wp-content/uploads/2013/04/imagens-lindas-6.jpg?0bce15" width="150" height="150" />
      <span>
          <img src="http://icons.iconarchive.com/icons/icojam/blue-bits/24/symbol-check-icon.png" />
      </span>
    </li>
</div>
<br><br><br><br><br><br><br><br><br>
<div class="img_info">
  <div id="content_1" class="content hidden">content1</div>
        <div id="content_2" class="content hidden">content2</div>
      </div>

CSS

.images_list li {
    list-style: none;
    float: left;
    width: 150px;
    height: 150px;
    margin-right: 10px;
}

.images_list li span {
    display:none;
    position:absolute;
    top:0px;
    left:0px;
    width:24px;
    height:24px;    
}

.border {
    border: 6px solid #D8D8D8;
    background: url(upload/check.jpg);
}
.selected {
    border: 6px solid green;
    position:relative;
}
.hidden {
    display:none;
}
.images_list li.selected span {
    display:block;
}

JS:这是我的JS,我遇到了问题。我希望有人可以帮助我,谢谢!

$('.images_list li').click(function() {
            $('.images_list .selected').removeClass('selected');
            $(this).toggleClass('selected');
            var clicked = $(this).attr('title');
            $("#"+clicked).removeClass("hidden").siblings().addClass("hidden");
        });

你可以在这里看到我的小提琴:http://jsfiddle.net/jasonc21/59swswz7/

1 个答案:

答案 0 :(得分:1)

完全注释掉removeClass行。

$('.images_list li').click(function() {
            // Left the following in, in case later you want to make it single again.
			// $('.images_list .selected').removeClass('selected');
			$(this).toggleClass('selected');
			var clicked = $(this).attr('title');
			$("#"+clicked).removeClass("hidden").siblings().addClass("hidden");
		});
.images_list li {
	list-style: none;
	float: left;
	width: 150px;
	height: 150px;
	margin-right: 10px;
}

.images_list li span {
    display:none;
	position:absolute;
    top:0px;
    left:0px;
	width:24px;
	height:24px;    
}

.border {
	border: 6px solid #D8D8D8;
	background: url(upload/check.jpg);
}
.selected {
	border: 6px solid green;
    position:relative;
}
.hidden {
    display:none;
}
.images_list li.selected span {
    display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="images_list">
  <li class="border" title="content_1">
      <img src="http://www.p69.com.br/wp-content/uploads/2013/04/imagens-lindas-6.jpg?0bce15" width="150" height="150" />
      <span>
          <img src="http://icons.iconarchive.com/icons/icojam/blue-bits/24/symbol-check-icon.png" />
      </span>
    </li>
  <li class="border" title="content_2">
      <img src="http://www.p69.com.br/wp-content/uploads/2013/04/imagens-lindas-6.jpg?0bce15" width="150" height="150" />
      <span>
          <img src="http://icons.iconarchive.com/icons/icojam/blue-bits/24/symbol-check-icon.png" />
      </span>
    </li>
</div>
<br><br><br><br><br><br><br><br><br>
<div class="img_info">
  <div id="content_1" class="content hidden">content1</div>
        <div id="content_2" class="content hidden">content2</div>
      </div>