以下HTML:
<div id="catestory">
<div class="content">
<h2>some title here</h2>
<p>some content here</p>
</div>
<div class="content">
<h2>some title here</h2>
<p>some content here</p>
</div>
<div class="content">
<h2>some title here</h2>
<p>some content here</p>
</div>
</div>
当鼠标悬停div的内容时,它的backgroundColor和h2(在这个div中)backgroundColor改变(就像CSS:hover)
我知道这可以使用CSS(:hover)在现代浏览器中执行此操作,但IE6不起作用。
如何使用JavaScript(不是jQuery或其他JS框架)来做到这一点?
编辑:如何更改h2 backgroundColor
答案 0 :(得分:60)
var div = document.getElementById( 'div_id' );
div.onmouseover = function() {
this.style.backgroundColor = 'green';
var h2s = this.getElementsByTagName( 'h2' );
h2s[0].style.backgroundColor = 'blue';
};
div.onmouseout = function() {
this.style.backgroundColor = 'transparent';
var h2s = this.getElementsByTagName( 'h2' );
h2s[0].style.backgroundColor = 'transparent';
};
答案 1 :(得分:9)
在代码中添加/更改元素的样式是一种不好的做法。今天你要改变背景颜色,明天你想改变背景图像,明天之后你决定改变边界< / em>的。
每次编辑代码只是因为设计要求的变化很痛苦。此外,如果您的项目将增长,更改js文件将更加痛苦。更多代码,更痛苦。
尝试消除硬编码样式的使用,这将节省您的时间,如果您做得对,您可以要求向其他人执行“更改颜色”任务。
因此,您可以在节点上添加/删除CSS类,而不是更改样式的直接属性。在您的特定情况下,您只需要为父节点 - “div”执行此操作,然后通过CSS设置子节点的样式。因此,无需将特定样式属性应用于DIV和H2。
还有一个推荐点。尽量不要连接硬编码的节点,但使用一些语义来做到这一点。例如:“将事件添加到具有类'content'的所有节点。
总之,以下是我将用于此类任务的代码:
//for adding a css class
function onOver(node){
node.className = node.className + ' Hover';
}
//for removing a css class
function onOut(node){
node.className = node.className.replace('Hover','');
}
function connect(node,event,fnc){
if(node.addEventListener){
node.addEventListener(event.substring(2,event.length),function(){
fnc(node);
},false);
}else if(node.attachEvent){
node.attachEvent(event,function(){
fnc(node);
});
}
}
// run this one when window is loaded
var divs = document.getElementsByTagName("div");
for(var i=0,div;div =divs[i];i++){
if(div.className.match('content')){
connect(div,'onmouseover',onOver);
connect(div,'onmouseout',onOut);
}
}
你的CSS应该是这样的:
.content {
background-color: blue;
}
.content.Hover{
background-color: red;
}
.content.Hover h2{
background-color : yellow;
}
答案 2 :(得分:5)
通过DOM访问要更改的元素,例如在事件处理程序中使用document.getElementById()
或this
,并更改该元素中的样式:
document.getElementById("MyHeader").style.backgroundColor='red';
修改强>
您也可以使用getElementsByTagName,(未经测试)示例:
function colorElementAndH2(elem, colorElem, colorH2) {
// change element background color
elem.style.backgroundColor = colorElem;
// color first contained h2
var h2s = elem.getElementsByTagName("h2");
if (h2s.length > 0)
{
hs2[0].style.backgroundColor = colorH2;
}
}
// add event handlers when complete document has been loaded
window.onload = function() {
// add to _all_ divs (not sure if this is what you want though)
var elems = document.getElementsByTagName("div");
for(i = 0; i < elems.length; ++i)
{
elems[i].onmouseover = function() { colorElementAndH2(this, 'red', 'blue'); }
elems[i].onmouseout = function() { colorElementAndH2(this, 'transparent', 'transparent'); }
}
}
答案 3 :(得分:3)
<script type="text/javascript">
function enter(elem){
elem.style.backgroundColor = '#FF0000';
}
function leave(elem){
elem.style.backgroundColor = '#FFFFFF';
}
</script>
<div onmouseover="enter(this)" onmouseout="leave(this)">
Some Text
</div>
答案 4 :(得分:1)
这个可能有点奇怪,因为我真的不是一个认真的程序员,我在编程中发现青霉素的发明方式 - 纯属意外。那么如何在鼠标悬停时更改元素?使用:hover
属性,就像使用a
元素一样。
示例:
div.classname:hover
{
background-color: black;
}
这会更改任何div
类classname
,以便在mousover上设置黑色背景。您基本上可以更改任何属性。在IE和Firefox中测试
快乐的节目!
答案 5 :(得分:0)
要在没有jQuery或任何其他库的情况下执行此操作,您需要将onMouseOver和onMouseOut事件附加到每个div并更改事件处理程序中的样式。
例如:
var category = document.getElementById("catestory");
for (var child = category.firstChild; child != null; child = child.nextSibling) {
if (child.nodeType == 1 && child.className == "content") {
child.onmouseover = function() {
this.style.backgroundColor = "#FF0000";
}
child.onmouseout = function() {
// Set to transparent to let the original background show through.
this.style.backgroundColor = "transparent";
}
}
}
如果您的h2没有设置自己的背景,div背景将显示并为其着色。
答案 6 :(得分:0)
如果您愿意在文档中插入非语义节点,可以通过将div包装在假A标签中,以仅与CSS兼容的方式执行此操作。
<style type="text/css">
.content {
background: #ccc;
}
.fakeLink { /* This is to make the link not look like one */
cursor: default;
text-decoration: none;
color: #000;
}
a.fakeLink:hover .content {
background: #000;
color: #fff;
}
</style>
<div id="catestory">
<a href="#" onclick="return false();" class="fakeLink">
<div class="content">
<h2>some title here</h2>
<p>some content here</p>
</div>
</a>
<a href="#" onclick="return false();" class="fakeLink">
<div class="content">
<h2>some title here</h2>
<p>some content here</p>
</div>
</a>
<a href="#" onclick="return false();" class="fakeLink">
<div class="content">
<h2>some title here</h2>
<p>some content here</p>
</div>
</a>
</div>
答案 7 :(得分:0)
您可以尝试使用此脚本。 :)
<html>
<head>
<title>Div BG color</title>
<script type="text/javascript">
function Off(idecko)
{
document.getElementById(idecko).style.background="rgba(0,0,0,0)"; <!--- Default --->
}
function cOn(idecko)
{
document.getElementById(idecko).style.background="rgb(0,60,255)"; <!--- New content color --->
}
function hOn(idecko)
{
document.getElementById(idecko).style.background="rgb(60,255,0)"; <!--- New h2 color --->
}
</script>
</head>
<body>
<div id="catestory">
<div class="content" id="myid1" onmouseover="cOn('myid1'); hOn('h21')" onmouseout="Off('myid1'); Off('h21')">
<h2 id="h21">some title here</h2>
<p>some content here</p>
</div>
<div class="content" id="myid2" onmouseover="cOn('myid2'); hOn('h22')" onmouseout="Off('myid2'); Off('h22')">
<h2 id="h22">some title here</h2>
<p>some content here</p>
</div>
<div class="content" id="myid3" onmouseover="cOn('myid3'); hOn('h23')" onmouseout="Off('myid3'); Off('h23')">
<h2 id="h23">some title here</h2>
<p>some content here</p>
</div>
</div>
</body>
<html>
答案 8 :(得分:0)
只需在javaScript上使用一个函数并将其命名为onclick
即可 <script type="text/javascript">
function change()
{
document.getElementById("catestory").style.backgroundColor="#666666";
}
</script>
<a href="#" onclick="change()">Change Bacckground Color</a>