這已修復。這是糟糕的編程。我正在調用這個函數兩次。所以它正在刪除類,但是當函數第二次運行時它正在讀取類。對不起,我的編程不好,謝謝大家的幫助。我愛所有有幫助的人都喜歡stackoverflow!jQuery hasClass返回true,但removeClass不起作用?
好吧,我一直在試圖找出一些東西,但它可能是我的冷,阻止我解決它。我正在做的是當用戶選擇一些它增加了一個類。但如果他們再次選擇它,我想刪除這個類。所以我查看了元素hasClass是否返回true,但是當我刪除類時,它什麼都沒做......
修改JS看起來像這樣。爲了表明我可以看到Selectedindex和index匹配,並且出於某種原因,正在跳過/忽略removeClass for「option-select」。我可以改變它說removeClass(「選項」),它工作得很好,但我不能removeClass(「選項選擇」)選擇的元素時
$(obj.find('.option')).each(function(Selectedindex) {
if ($(this).hasClass('option-selected') && Selectedindex == index) {
$(this).removeClass('option-selected');
console.log(Selectedindex+" == "+index);
}
});
指數傳遞給函數。
這裏是一些HTML
<div id="MultipleSelect-HTML" class="dropdown container" multiple="multiple" style="width: 100%;">
<ul class="options" style="width: 100%; display: block; position: relative;">
<li>
<a class="option option-selected">
<input class="option-value" type="hidden" value="0">
<img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-facebook.png">
<label class="option-text" style="cursor:pointer;">Facebook</label>
<small class="option-description desc">Check out my Facebook page!</small>
</a>
</li>
<li>
<a class="option option-selected">
<input class="option-value" type="hidden" value="1">
<img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-twitter.png">
<label class="option-text" style="cursor:pointer;">Twitter</label>
<small class="option-description desc">Check out my Twitter page!</small>
</a>
</li>
<li>
<a class="option">
<input class="option-value" type="hidden" value="2">
<img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-linkedin.png">
<label class="option-text" style="cursor:pointer;">LinkedIn</label>
<small class="option-description desc">Check out my LinkedIn page!</small>
</a>
</li>
<li>
<a class="option"> <input class="option-value" type="hidden" value="3">
<img class="option-image" src="http://cdn1.iconfinder.com/data/icons/inside/PNG/032x032/icontexto-inside-flickr.png">
<label class="option-text" style="cursor:pointer;">Flickr</label>
<small class="option-description desc">I don't have a flicker Page :(</small>
</a>
</li>
</ul>
</div>
的請保持所有這些信息都使用JavaScript動態
感謝您的幫助,您可以提供生成的頭腦。
你可以發佈你的HTML。此外,您正在檢查'選擇選項'並刪除'dd-option-selected' - 是否正確? –
你正在檢查的課程和你正在移除的課程是不同的。 – Bruno
是的,修好了,謝謝...但它仍然無法工作。任何其他想法? –