[英]JQuery addclass and Remove class applying to for each variables not working
在我的頁面中,我使用了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')。each(function(){不起作用,圖像的邊框也沒有應用。任何人都可以幫助我解決此問題。在此先感謝
您的意思是寫var currentId = $(clickObject).attr('id')
但是,這樣做會更容易:
function swapBorder(elem) {
$("#containerborder a").removeClass('active-class');
$(elem).addClass('active-class');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.