在我的页面中,我使用了jquery addclass并删除了类功能来为所选图像添加边框。但是图像在for循环中。所以它不适合我,我尝试了各种编码。但我没有得到预期的结果。我用的编码
for循环中的四张图片列表
<div id="containerborder">
<?php foreach ($images as $image) { ?>
<a href="javascript:;" title="<?php echo $heading_title; ?>" class="colorboxs" rel="<?php echo $image['popup'];?>"onclick="document.getElementById('image').src=this.rel"> <img src="<?php echo $image['thumb']; ?>" title="<?php echo $heading_title; ?>" alt="<?php echo $heading_title; ?>" id="<?php echo $image['popup']; ?>" onclick="swapborder(this.id);"/>
</a>
<?php } ?>
JQuery功能
<script>
$(function() {
$('#contianer a').click(function() { return swapborder(this); });
});
//
function swapborder(clickObject) {
var currentId=clickObject;
alert(currentId);
$('#containerborder a').each(function() {
if ($(this).attr('id')==currentId) {
$(this).addClass('active-class');
}
else { $(this).removeClass('active-class'); }
});
return false;
}
</script>
它显示警报中的ID。但在那之后 $('#containerborder a')。每个(function(){不起作用,图像的边框不适用。任何人都可以帮我解决这个问题。谢谢预先
答案 0 :(得分:1)
你的意思是写var currentId = $(clickObject).attr('id')
然而,这样做会更容易:
function swapBorder(elem) {
$("#containerborder a").removeClass('active-class');
$(elem).addClass('active-class');
}