2016-01-01 259 views
0

我想點擊我的按鈕後有懸停效果。我曾經這樣做過jQuery元素數組,可能有問題。我的代碼在這裏,所以你可以看到它是如何工作的:點擊後jQuery懸停效果不起作用

什麼是錯的?

var lastclick = 0; 
 
function clicked(x) 
 
{ 
 
\t if(lastclick!=0) $("#button")[lastclick-1].mouseleave(); 
 
\t $("#button")[x-1].mouseenter(); 
 
\t lastclick = x; 
 
} 
 

 
$(".1").click(function(){clicked(1);}); 
 
$(".2").click(function(){clicked(2);}); 
 
$(".3").click(function(){clicked(3);});
#button { 
 
    width: 400px; 
 
    height: 50px; 
 
    text-align: center; 
 
    background-color: #ebebeb; 
 
    margin: 20px; 
 
} 
 

 
#button:hover { 
 
    cursor: pointer; 
 
    background-color: gray; 
 
}
<div id="button" class="1"> 
 
1 
 
</div> 
 
<div id="button" class="2"> 
 
2 
 
</div> 
 
<div id="button" class="3"> 
 
3 
 
</div>

JSFiddle鏈接

+0

1.修正的鏈接。 2.直接將相關代碼提交給問題,而不僅僅是在小提琴中。 –

+0

請描述所需的行爲。 「點擊後懸停效果」 - 意思是你想讓它改變顏色和**保持改變**,不管懸停嗎?或者你想要一個不同於你現有的懸停效果?或者是其他東西? –

+0

你的代碼真的很糟糕。 Id必須是唯一的頁面範圍(一個元素 - 一個唯一的ID),類名稱不應以數字開頭。 –

回答

0

我還是你想要什麼有點不清楚。但在這裏,如果我理解正確的話。正如其他人提到你不能用數字或一些符號開始一個id。其次,id必須是獨一無二的,就像兩個人一樣,沒有兩個人擁有相同的id。

注意:您不需要JavaScript。

.button{ 
 
    display: none; 
 
} 
 
.button + label{ 
 
    width: 400px; 
 
    height: 50px; 
 
    display: block; 
 
    text-align: center; 
 
    background-color: #ebebeb; 
 
    
 
    margin: 20px; 
 
    cursor: pointer; 
 
    
 
    -webkit-transition: background-color 0.25s; 
 
     -moz-transition: background-color 0.25s; 
 
      transition: background-color 0.25s; 
 
} 
 
.button:checked + label{ 
 
    
 
    background-color: gray; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<input type="radio" class="button" name="set" id="button1"/> 
 
<label for="button1">1</label> 
 

 

 
<input type="radio" class="button" name="set" id="button2"/> 
 
<label for="button2">2</label> 
 

 

 
<input type="radio" class="button" name="set" id="button3"/> 
 
<label for="button3">3</label>

+0

我有我的原始按鈕3種不同的類,所以唯一的辦法就是模擬點擊後懸停動作。只有一個按鈕必須一次懸空。 – Artimal

+0

你的意思是你的原始按鈕裏有3個不同的類別?那沒有意義。告訴我你想要什麼! – Gacci

+0

Sory,不同的divs和每個人都有differetn類:)我想模擬懸停行動,因爲在CSS我有︰# \t width:5vw; } #button:hover #buttontext { \t transition:all 200ms ease; \t顏色:rgba(50,50,50,1); } #button:hover .ikonka { \t transition:all 200ms ease; fill:#ffffff; } 因此很難一次更改3個類,因此我想在設置不同的懸停後單擊並清理懸停後模擬鼠標懸停。 – Artimal