目前,我是Jquery的新手,但是在这种情况下,基于api.jquery.com的以下代码应该可以工作:
<a class="btindex">Startseite</a>
$('.btindex').click(function(){$(this).attr('href','index.html')});
$('.btindex').on('mouseover',function(){$(this).css('background-color':'#f2ab1e')});
$('.btindex').on('mouseout',function(){$(this).css('background-color':'#f0c911')});
我也把它们写成一个,因为它对我来说更清楚,但是,下面是代码:
$('.btindex').click(function(){$(this).attr('href','index.html')}).on('mouseover',function(){$(this).css('background-color':'#f2ab1e')}).on('mouseout',function(){$(this).css('background-color':'#f0c911')});
此外,在本例中没有必要使用css文件:
.btindex{
cursor: pointer;
background-color:#f0c911;
border:1px solid #e65f44;
color:#c92200;
font-weight:bold;
font-style:italic;
font-size: 150%;
height:10%;
line-height:250%;
padding: auto;
position: fixed;
visibility: hidden;
width:22%;
text-decoration:none;
text-align:center;
}
我希望能得到快速的答案,如果不是的话,我一定会回答的。在任何情况下,我都会构建一个支持这里的小提琴:)
发布于 2014-07-20 00:55:43
通常情况下,避免使用css()
设置内联样式和简单地向控件样式添加和移除类更容易
如果您知道必须恢复到原来的状态,那么用css规则设置所需的时间要比将重置回原来的css属性值所需的JS添加的时间要少。
CSS
.btindex.hovered{
background-color:#f2ab1e;
}
JS
$('.btindex').hover(function(){
$(this).toggleClass('hovered');
});
只有一个回调的hover()
将同时涵盖mouseenter
和mouseleave
事件。
发布于 2014-07-20 00:12:40
在你应该有逗号的地方,你有结肠--应该是:
$('.btindex').on('mouseover',function(){$(this).css('background-color','#f2ab1e')});
$('.btindex').on('mouseout',function(){$(this).css('background-color','#f0c911')});
更新小提琴:http://jsfiddle.net/8Hbnk/2/
或者您可以将对象中的属性传递给css()
,小心地使用camelcase作为属性名(例如,backgroundColor
而不是background-color
):
$('.btindex').on('mouseover',function(){$(this).css({backgroundColor:'#f2ab1e'})});
$('.btindex').on('mouseout',function(){$(this).css({backgroundColor:'#f0c911'})});
https://stackoverflow.com/questions/24846146
复制相似问题