[英]Changing elements opacity using jQuery
單擊一個按鈕后,我需要更改所有按鈕的不透明度(被單擊的按鈕除外)。
這是HTML代碼:
<button data-target="Section1" class="metro-button">Button1</button>
<button data-target="Section2" class="metro-button">Button2</button>
<button data-target="Section3" class="metro-button">Button3</button>
和jQuery代碼:
$(".metro-button").click(function(){
var buttons = document.getElementsByClassName("metro-button");
for(i = 0 ; i < buttons.length ; i++) {
if ($(this).attr('data-target') != buttons[i].attr('data-target')) {
buttons[i].animate({"opacity" : 0.3});
}
}
});
問題是什么 ?
任何想法,將不勝感激。
堅持使用jQuery
$(".metro-button").click(function(){
$(".metro-button").not(this).animate({"opacity" : 0.3});
$(this).animate({"opacity" : 1});
});
也可能是一個解決方案,具體取決於您的HTML結構:
$(".metro-button").click(function () {
$(this).animate({
"opacity": 1
}).siblings('.metro-button').animate({
"opacity": .3
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.