我对JS非常陌生。我的要求很简单,在鼠标上改变文本的颜色。我已经创建了两个JS函数:第一个用于MouseOver,第二个用于MouseOut。我可以在一个单独的JS函数中完成吗?我还有其他的文字。
JavaScript
function highlightBG(element) {
document.getElementById('element').className='AttachOnMouseOverText';
}
function highlightOutBG(element){
document.getElementById('element').className='AttachOnMouseOutText';
}HTML代码:
<td align="center" id="element">
<img name="folder" onMouseOver="highlightBG();return true;" onMouseOut="highlightOutBG();return true;">
<br>Add Folder
</td>发布于 2014-02-28 19:21:58
你可以在这里找到答案,就像你问的那样,使用pure-js:
HTML:
<div id="element" class="AttachOnMouseOutText" onMouseOver="highlightBG();return true;" onMouseOut="highlightOutBG();return true;">Hidden text</div>CSS:
.AttachOnMouseOverText {
color: white;
}
.AttachOnMouseOutText {
color: black;
}Javascript:
function highlightBG() {
document.getElementById('element').className='AttachOnMouseOverText';
}
function highlightOutBG(){
document.getElementById('element').className='AttachOnMouseOutText';
}你可以看到一个使用CSS :hover here的例子。
编辑
如果你想要一个单独的函数来处理这件事,可以试试下面这样的方法:
function highlightBG(elementName, isIn) {
if (isIn)
document.getElementById(elementName).className = 'AttachOnMouseOverText';
else
document.getElementById(elementName).className = 'AttachOnMouseOutText';
}发布于 2014-02-28 19:16:49
通过使用css,这很简单:
selector:hover
{
color:red;
}您也可以使用jquery来实现这一点
$("selector").on( "mouseover", function() {
$( this ).css( "color", "red" );
});发布于 2014-02-28 19:17:06
如果你需要改变一个链接的悬停,那么一定要在CSS中使用:hover,这将是最有效的方法。
但是,如果您希望将其添加到非链接元素中,则可能会在IE7和8中导致问题。请查看Google Best Practices,特别是关于:hover的部分。如果是这样的话,JS就是一种方法。
https://stackoverflow.com/questions/22093307
复制相似问题