简体   繁体   English

在 JS 中更改鼠标悬停时文本的颜色

[英]change the color of text on mouseover in JS

I am very new to JS.我对 JS 很陌生。 My requirement is very simple, to change the color of Text on Mouse Over.我的要求很简单,在鼠标悬停时更改文本的颜色。 I have created 2 JS functions : 1st for MouseOver and 2nd for MouseOut.我创建了 2 个 JS 函数:第一个用于 MouseOver,第二个用于 MouseOut。 Can I do it in one single JS function.我可以在一个 JS 函数中完成它吗? I have other Text also.我也有其他文本。

JavaScript JavaScript

function highlightBG(element) {  
    document.getElementById('element').className='AttachOnMouseOverText';   
}
function highlightOutBG(element){
    document.getElementById('element').className='AttachOnMouseOutText';
}

HTML code : HTML代码:

<td align="center" id="element">
    <img name="folder" onMouseOver="highlightBG();return true;" onMouseOut="highlightOutBG();return true;">
    <br>Add Folder
</td>

You can find here the answer using pure-js as you asked :您可以在此处使用 pure-js 找到答案,如您所问:

HTML : HTML :

<div id="element" class="AttachOnMouseOutText" onMouseOver="highlightBG();return true;" onMouseOut="highlightOutBG();return true;">Hidden text</div>

CSS : CSS :

.AttachOnMouseOverText {
    color: white;
}

.AttachOnMouseOutText {
    color: black;
}

Javascript : Javascript :

function highlightBG() {  
    document.getElementById('element').className='AttachOnMouseOverText';   
}
function highlightOutBG(){
    document.getElementById('element').className='AttachOnMouseOutText';
}

You can see here an example using CSS :hover state.你可以在这里看到一个使用 CSS :hover状态的例子。

EDIT编辑

If you want a single function to handle this, try someting like :如果您想要一个函数来处理这个问题,请尝试以下操作:

function highlightBG(elementName, isIn) {
    if (isIn)
        document.getElementById(elementName).className = 'AttachOnMouseOverText';
    else
        document.getElementById(elementName).className = 'AttachOnMouseOutText';
}

this is simple by using css:使用css很简单:

selector:hover
{
  color:red;
}

And you can also use jquery for this您也可以为此使用 jquery

$("selector").on( "mouseover", function() {
  $( this ).css( "color", "red" );
});

If you need the hover change on a link then definitely use a :hover in CSS, it will be the most efficient way.如果您需要更改链接上的悬停,那么肯定在 CSS 中使用 :hover,这将是最有效的方法。

However if you are looking to add it to a non-link element it can cause issues in IE7 and 8. Have a look at Google Best Practices , in particular the section about :hover.但是,如果您希望将其添加到非链接元素,则可能会导致 IE7 和 8 出现问题。请查看Google 最佳实践,特别是有关 :hover 的部分。 If that is the case then JS is a way to do it.如果是这种情况,那么 JS 是一种方法。

It might be easier to use jquery to do what you want, if you are using javascript you might just as well make use of jquery.使用 jquery 做你想做的事情可能更容易,如果你使用 javascript,你也可以使用 jquery。 Create a css class to represent the color you want to change the text to, for example例如,创建一个 css 类来表示您要将文本更改为的颜色

.green{
    color: green;
}

Change your HTML to将您的 HTML 更改为

<td align="center" id="element">
    <img name="folder" />
    <br>Add Folder
</td>

And add some jquery to add your css class when you move your mouse over 'element', for example例如,当您将鼠标移到“元素”上时,添加一些 jquery 来添加您的 css 类

$("#element").mouseover(function(){
    $(this).addClass("green");
});

If you want to change the color back when the mouse leaves the area, you can just remove the class again.如果您想在鼠标离开该区域时将颜色更改回来,您可以再次删除该类。 For example例如

$( "#element" ).mouseleave(function() {
    $(this).removeClass("green");
});

Here is the HTML (with an inline ID of "practice"):这是 HTML(带有“practice”的内联 ID):

<h1 id="practice">Hello!</h1>

Here is the vanilla JavaScript (using a generic function and a callback):这是 vanilla JavaScript(使用通用函数和回调):

document.getElementById("practice").addEventListener("mouseover", function() {
  document.getElementById("practice").style.color = "pink";
});
document.getElementById("practice").addEventListener("mouseout", function() {
  document.getElementById("practice").style.color = "yellow";
});

Mousing over changes the HTML text to yellow;鼠标悬停将 HTML 文本更改为黄色; removing the mouse from the area returns the HTML text to black.从该区域移除鼠标会使 HTML 文本变为黑色。

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

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