OnClick样式更改

时间:2013-07-15 10:38:24

标签: javascript

当我点击它们时,我正试图在链接上保留悬停样式状态,有点像标签效果。它似乎没有工作,希望有人可以帮助我。

CSS:

.nav {
position: absolute;
z-index: 1000;
font-family: Asap;
right: 100px;
top: 70px;

}

.nav a, .nav a:active, .nav a:visited {
font-family: 'Asap', sans-serif;
color: #fff;
font-size: 12px;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 1px;
text-shadow: black 0.1em 0.1em 0.2em;
}

.nav a:hover {
font-family: Asap;
color: #fff;
font-size: 12px;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 1px;
border-top: 2px solid #fff;
padding-top: 5px;
}

.nav2 a {
font-family: Asap;
color: #fff;
font-size: 12px;
text-decoration: none;
text-transform: uppercase;
letter-spacing: 1px;
border-top: 2px solid #fff;
padding-top: 5px;
}

脚本:

$('div.nav > a').click(function(){
$(this).parent().removeClass('nav').addClass('nav2');
});

HTML:

<div class="nav" onclick="javascript:showlayer('myName')">
<span class="btn1"><a href="#page-1" title="Design">Design</a>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span class="btn2"><a href="#page-2" title="Neighbourhood">Neighbourhood</a> 
&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span class="btn3"><a href="#page-3" title="Media">Media</a>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span class="btn4"><a href="#page-4" title="Media">Developer</a>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<span class="btn5"><a href="#page-5" title="Media">Contact</a>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<br><br><br><br><br><br><br><br><br><br>
</div>

1 个答案:

答案 0 :(得分:1)

你可以通过在onClick上声明一个不同的样式来做纯粹的Javascript。

var change = document.getElementById("Link Id");  //Define the link in a variable
change.onClick=function() {           //Activate the function when clicked
  change.style.fontFamily=("Asap");
  change.style.color=("#fff");
  change.style.fontSize=("12px");
  change.style.textDecoration=("none");         //Change the link attributes
  change.style.textTransform=("uppercase");
  change.style.letterSpacing=("1px");
  change.style.borderTop=("2px solid #fff");
  change.style.paddingTop= ("5px");
}

单击链接后,应将链接样式更改为悬停样式。只需确保为链接指定了ID。确保你知道像src标签这样的基本Javascript来调用它:

<script type = "text/javascript" src = "externaljavascript.js"></script>  

不要忘记不要在外部脚本中使用标记。