![](/img/trans.png)
[英]On click event add Class to each HTML Element from different levels,
[英]Add different class to each image with different id after drop event
我在一个容器中有5张图像,我必须将其放到另一个容器中以完成图像。 将图像放入另一个容器后,我想根据其ID向该图像添加一个不同的类。 这样该图像就可以粘贴到该容器中存在的先前图像上。
我能够实现拖放事件,并在拖放事件后向每个图像添加一个类。 我需要在代码中进行哪些更改,以便根据其ID向每个图像添加不同的类
我的密码
<style>
.add-style {
position:absolute;
}
.north-style {
width: 375px;top: 10px;left: 11px;
}
.south-style {
width: 375px;top: 158px;left: 11px;
}
.east-style {
width: 163px;top: 10px;left: 223px;height: 250px;
}
.west-style {
width: 163px;top: 9px;left: 11px;height: 249px;
}
.center-style {
width: 126px;top: 71px;left: 135px;
}
</style>
<script>
$(function() {
$( "#sortable1" ).sortable({
connectWith: "div"
});
$( "#sortable2" ).sortable({
connectWith: "div",
stop: function( event, ui ) {
ui.item.addClass( "add-style" )
}
});
$( "#sortable1, #sortable2" ).disableSelection();
});
</script>
代码主体
<body>
<div class="row-fluid" >
<div class="span4">
<div class="span1"></div>
<div id="sortable1" class="well sidebar-nav sidebar-nav-fixed span11">
<legend>Collect Coupon parts</legend>
<span>1st part</span>
<img id="north-img" class="ui-state-highlight" src="images/demo/north.png" >
<span>2nd part</span>
<img id="south-img" class="ui-state-highlight" src="images/demo/south.png" >
<span>3rd part</span>
<img id="east-img" class="ui-state-highlight" src="images/demo/east.png" >
<span">4th part</span>
<img id="west-img" class="ui-state-highlight" src="images/demo/west.png" >
<span>5th part</span>
<img id="center-img" class="ui-state-highlight" src="images/demo/center.png">
</div>
</div>
<div id="sortable2" class=" well sidebar-nav sidebar-nav-fixed span8">
<legend>Canvas section</legend>
</div>
</div>
</body>
首先将您的课程更改为以下内容。
显示了两个示例,也更改为其他3个。
.north-img-style {
width: 375px;top: 10px;left: 11px;
}
.south-img-style {
width: 375px;top: 158px;left: 11px;
}
然后在代码的这一部分中添加这两行
stop: function( event, ui ) {
var theID = ui.item.attr('id');
ui.item.addClass(theID + '-style');
}
var theID
获取元素的id
,然后我们只添加具有相同ID的Class,但向其中添加-style。 因此,这将适用于您所有五个不同的类。
编辑
是的,您一次可以添加多个课程。 像这样用逗号分开:
var theID = ui.item.attr('id');
ui.item.addClass(theID + '-style', 'add-style');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.