我想为每次点击类似元素添加一个属性/值,以便在其他地方使用该值。我想用它的代码就像
<div class="productbox"><img src="image.png"></div>
<div class="productbox"><img src="image2.png"></div>
<div class="productbox"><img src="image3.png"></div>
<div class="differentcontainer">
<!-- the value of the image shall be put in here as <img src="..."> -->
</div>
$(".productbox").click(function() {
var imgname = $(this).next(img).value;
$(".differentcontainer").html(imgname);
});
所以我想获得comoplete img-tag的值,并在点击其他元素后使用它。因为我已经使用了jquery,所以最有可能坚持它。
谢谢!
答案 0 :(得分:2)
jQuery syntax
专为选择HTML
元素而量身定做 对元素执行某些操作。基本语法是:$(选择器).action();
用于定义/访问jQuery的
$
符号。
(selector)
以“查询(或查找)”HTML元素。要对元素执行的jQuery
action()
$(".productbox").click(function() {
var imgname = $(this).html();
$(".differentcontainer").html(imgname);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="productbox"><img style="width:50px; height:50px" src="https://i2.wp.com/mightywidow.com/wp-content/uploads/2016/12/11519100485_ddfd5be329_z.jpg?fit=640%2C361&ssl=1" title="1"></div>
<div class="productbox"><img style="width:50px; height:50px" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSnXSQpzvR2frx8nzq-rxxQZsOjPtRWNVVRwoU7-NsUAtGYUOom" title="2"></div>
<div class="productbox"><img style="width:50px; height:50px" src="https://s-media-cache-ak0.pinimg.com/736x/e6/63/d0/e663d0bf3d57da87ef9992cddd5af05c--kindness-ideas-acts-of-kindness.jpg" title="3"></div>
<div class="differentcontainer">
<!-- the value of the image shall be put in here as <img src="..."> -->
</div>
答案 1 :(得分:0)
Juts在div中获取html,如下所示:
$(".productbox").click(function() {
var imgname = $(this).html();
$(".differentcontainer").empty();
$(".differentcontainer").html(imgname);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="productbox"><img src="http://via.placeholder.com/350x150"></div>
<div class="productbox"><img src="http://via.placeholder.com/140x100"></div>
<div class="productbox"><img src="http://via.placeholder.com/200x100"></div>
<div class="differentcontainer">
<!-- the value of the image shall be put in here as <img src="..."> -->
</div>