2016-01-01 345 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