简体   繁体   English

Onmouseover在JavaScript中添加边框

[英]Onmouseover add border in JavaScript

I want to add a top border to my navbar buttons when the mouse is hovered over them and then back to its original state when mouse is off the elements. 当鼠标悬停在导航栏按钮上方时,我想在其顶部添加边框,然后在鼠标离开元素时又回到其原始状态。

However my code does not seem to be working, I am using a function to do this in JavaScript as follows: 但是我的代码似乎不起作用,我正在使用一个函数在JavaScript中执行以下操作:

<script type = "text/javascript">
function border(element){
element.style.borderTop ='3px solid'; 
element.style.borderColor = '#d22b2b';
}
</script>

and the HTML: 和HTML:

<ul align="center">
<li onmouseover="border(this)"><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Services</a></li>
<li><a href="">Portfolio</a></li>
<li><a href="">Contact</a></li>
</ul>

With the above code nothing is happening at all when I hover the mouse over the first li element. 使用上面的代码,当我将鼠标悬停在第一个li元素上时,什么也没有发生。

I updated your code and managed to get it working. 我更新了您的代码,并设法使其正常运行。 Here is the jfiddle for it: http://jsfiddle.net/A2gkU/2/ 这是它的jfiddle: http : //jsfiddle.net/A2gkU/2/

The problem was that you had too much padding above/below the links, so even if there was a border showing up above, it got hidden by the padding. 问题是链接上方/下方的填充过多,因此,即使上方显示边框,填充也隐藏了边框。

HTML Codes: HTML代码:

<div id="navbar">
<ul align="center">
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Services</a></li>
<li><a href="">Portfolio</a></li>
<li><a href="">Contact</a></li>
</ul>
</div>

CSS codes: CSS代码:

#navbar{
background-color: #fff;
overflow:hidden;
-moz-box-shadow: 0px 1px 4px rgba(0,0,0,0.6);
-webkit-box-shadow: 0px 1px 4px rgba(0,0,0,0.6);
box-shadow: 0px 1px 4px rgba(0,0,0,0.6);
padding:5px;

}

#navbar ul {list-style:none; 
        text-align: center;
        margin: 5px;
        font-family: Segoe UI;
        font-size: 17px;
        font-weight: bold;  
}

#navbar ul li {
display: inline;    
}

#navbar ul li a {
text-decoration:none;
color:#302E2E;
    padding-left: 15px;
background-color: #fff; 

}

#navbar ul li a:hover { color: #F56; border-top: 3px solid #d22b2b;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM