Please help me out with this. I am trying to apply a CSS Style to this Javascript Code that I have.
I want it to contain Hover/or mouseover the color of the text be white for example and no underline or something like that.
Can someone give me an example of how to do that?
I am a beginner in this and don't know how to make something like this
var links = [ ['Home', '#1'], ['News', '#2'], ['Contact', '#3'], ['About', '#4'] ]; function makeNav( links ) { var nav = document.getElementById('myTopnav'); for ( var i = 0; i < links.length; i ++ ) { nav.innerHTML += '<a class="nav-link" href="' + links[i][1] + '">' + links[i][0] + '</a>' + ' '; } return nav; } makeNav( links );
body { background-image: url(backgroundimg.jpg); background-repeat: no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } #myTopnav { background: rgba(25, 25, 25, .2); height: 40px; top: 200px; position: absolute; width: 50%; z-index: 0; text-align: center; padding: 10px; word-spacing: 40px; font-size: 30px; color: white; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css.css"> <title></title> </head> <body> <div class="topnav" id="myTopnav"></div> <script src="jquery-3.2.1.min"></script> <script src="javascript.js"></script> </body> </html>
Changing the style of an element when you hover it does not require any “JavaScript magic”, this is a simple use case of CSS. You can define a class with the :hover peudoselector to change the style of an element when the mouse moves over it.
In your example, this would look something like this:
.nav-link:hover {
color: white;
}
var links = [ ['Home', '#1'], ['News', '#2'], ['Contact', '#3'], ['About', '#4'] ]; function makeNav( links ) { var nav = document.getElementById('myTopnav'); for ( var i = 0; i < links.length; i ++ ) { nav.innerHTML += '<a class="nav-link" href="' + links[i][1] + '">' + links[i][0] + '</a>' + ' '; } return nav; } makeNav( links );
body { background-image: url(backgroundimg.jpg); background-repeat: no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } #myTopnav { background: rgba(25, 25, 25, .2); height: 40px; top: 200px; position: absolute; width: 50%; z-index: 0; text-align: center; padding: 10px; word-spacing: 40px; font-size: 30px; color: white; } .nav-link:hover { color: white; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css.css"> <title></title> </head> <body> <div class="topnav" id="myTopnav"></div> <script src="jquery-3.2.1.min"></script> <script src="javascript.js"></script> </body> </html>
In CSS, use :hover
on your links .nav-link
like this:
var links = [ ['Home', '#1'], ['News', '#2'], ['Contact', '#3'], ['About', '#4'] ]; function makeNav( links ) { var nav = document.getElementById('myTopnav'); for ( var i = 0; i < links.length; i ++ ) { nav.innerHTML += '<a class="nav-link" href="' + links[i][1] + '">' + links[i][0] + '</a>' + ' '; } return nav; } makeNav( links );
body { background-image: url(backgroundimg.jpg); background-repeat: no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } #myTopnav { background: rgba(25, 25, 25, .2); height: 40px; top: 200px; position: absolute; width: 50%; z-index: 0; text-align: center; padding: 10px; word-spacing: 40px; font-size: 30px; color: white; } .nav-link:hover { color: #FFF; text-decoration: none; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css.css"> <title></title> </head> <body> <div class="topnav" id="myTopnav"></div> <script src="jquery-3.2.1.min"></script> <script src="javascript.js"></script> </body> </html>
You can accomplish this using plain old css. Note the a:hover
styles added to the css file.
var links = [ ['Home', '#1'], ['News', '#2'], ['Contact', '#3'], ['About', '#4'] ]; function makeNav( links ) { var nav = document.getElementById('myTopnav'); for ( var i = 0; i < links.length; i ++ ) { nav.innerHTML += '<a class="nav-link" href="' + links[i][1] + '">' + links[i][0] + '</a>' + ' '; } return nav; } makeNav( links );
body { background-image: url(backgroundimg.jpg); background-repeat: no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } #myTopnav { background: rgba(25, 25, 25, .2); height: 40px; top: 200px; position: absolute; width: 50%; z-index: 0; text-align: center; padding: 10px; word-spacing: 40px; font-size: 30px; color: white; } a:hover { color: #FFF; text-decoration: none; }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css.css"> <title></title> </head> <body> <div class="topnav" id="myTopnav"></div> <script src="jquery-3.2.1.min"></script> <script src="javascript.js"></script> </body> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.