gpt4 book ai didi

Javascript:更改

标题颜色 onmouseover()

转载 作者:太空宇宙 更新时间:2023-11-03 20:11:40 25 4
gpt4 key购买 nike

我正在尝试实现一些 native Javascript 代码,以便当您将鼠标悬停在标题上时,标题会改变颜色。

我会将所有 JS 代码放在 HTML 的底部,这样您就可以在上下文中看到它,但是我添加到我的 .js 文件中的代码如下:

//change <h2> on mouseover/mouseout
document.getElementsByClassName("heading2").onmouseover.style.color="red";

HTML 文档:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Session 5 - Dynamic Menu</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="dynamicmenu.js"></script>
</head>
<body>
<h1>Web Programming Languages</h1>
<h2>JavaScript</h2>
<p>
JavaScript (JS) is an interpreted computer programming language. As part of web browsers, implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content
</p>

<h2>PHP</h2>
<p>
PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. PHP is now installed on more than 244 million websites and 2.1 million web servers. Originally created by Rasmus Lerdorf in 1995, the
</p>

<h2>Ruby</h2>
<p>
Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.Ruby embodies syntax inspired by Perl with Smalltalk-like features and was also influenced by Eiffel and Lisp. It supports multiple programming paradigms, includin
</p>

<h2>ASP</h2>
<p>
Active Server Pages (ASP), also known as Classic ASP or ASP Classic, was Microsoft's first server-side script engine for dynamically generated web pages. Initially released as an add-on to Internet Information Services (IIS) via the Windows NT 4.0 Option Pack (ca. 1996), it was subsequently included as a free
</p>

<h2>Java Server Pages</h2>
<p>
Architecturally, JSP may be viewed as a high-level abstraction of Java servlets. JSPs are translated into servlets at runtime; each JSP's servlet is cached and re-used until the original JSP is modified.
</p>

</body>
</html>

Javascript 代码:

 //function to create dynamic menu...
function dynamicMenu() {
//Get all <h2> headings in to a container
var headings = document.getElementsByTagName("h2");

//Create <div> for menu
var menu = document.createElement("div");

//Set id attribute for <div>
menu.setAttribute("id", "navWrap")

//Create <ul> for menu list items
var menuUL = document.createElement("ul");

//Set id attribute for <ul>
menuUL.setAttribute("id", "mainNav");

//Append the <ul> to the <div> as a child
menu.appendChild(menuUL);

//Set up loop to populate menu
for (var i = 0; i < headings.length; i++) {

//Collect the text content of h2 headings
var linkText = headings[i].childNodes[0].nodeValue;

//Create <li> element
var menuULitem = document.createElement("li");

//Append <li> to <ul> as child
menuUL.appendChild(menuULitem);

//Create <a> tag element
menuAtag = document.createElement("a");

//Append <a> to <li> as child
menuULitem.appendChild(menuAtag);

//Set the <a> href attribute to point to the anchor tag in body of document
menuAtag.setAttribute("href", "#item" + i)

//Append link text as a hchild of <a>
var menuText = document.createTextNode(linkText);
menuAtag.appendChild(menuText);

//Create an anchor point for each <h2>
var anchor = document.createElement("a");

//Set anchor attribute name
anchor.setAttribute("name", "item" + i);

//Insert anchor in to DOM
document.body.insertBefore(anchor, headings[i]);

//Give headings a class attribute
headings[i].setAttribute("class", "heading2");

}

//Insert the Menu created in above loop in to the DOM
document.body.insertBefore(menu, document.body.firstChild);

//change <h2> on mouseover/mouseout
document.getElementsByClassName("heading2").onmouseover.style.color="red";


}//close function (dynamicMenu)

window.onload = dynamicMenu;

最佳答案

使用 CSS

h2:hover
{
color : red;
}

如果你不需要在鼠标移出时去除颜色使用

var headers = document.getElementsByTagName("h2");

for (var i in headers)
{
headers[i].onmouseover = function()
{
this.style.color = 'red';
}
}

关于Javascript:更改 <h2> 标题颜色 onmouseover(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31213306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com