Fiddle: /
As you can see from the fiddle, I want to change color of span when hover on it, but somehow even I hover any in the first three element, the hover event just apply for the last span.
HTML
<p class="p">
<span>Span 1</span>
</p>
<p class="p">
<span>Span 2</span>
</p>
<p class="p">
<span>Span 3</span>
</p>
<p class="p">
<span>Span 4</span>
</p>
jQuery:
$('.p').each(function() {
$span = $(this).children('span');
$span.hover(
function() {
$span.css('color', 'red');
},
function() {
$span.css('color', 'blue');
}
)
});
Fiddle: http://jsfiddle/vretc/
As you can see from the fiddle, I want to change color of span when hover on it, but somehow even I hover any in the first three element, the hover event just apply for the last span.
HTML
<p class="p">
<span>Span 1</span>
</p>
<p class="p">
<span>Span 2</span>
</p>
<p class="p">
<span>Span 3</span>
</p>
<p class="p">
<span>Span 4</span>
</p>
jQuery:
$('.p').each(function() {
$span = $(this).children('span');
$span.hover(
function() {
$span.css('color', 'red');
},
function() {
$span.css('color', 'blue');
}
)
});
Share
Improve this question
asked Apr 30, 2013 at 13:00
user2335889user2335889
2
- Does your actual markup has more than one span per paragraph? – Bergi Commented Apr 30, 2013 at 13:10
- 1 Is there any reason why you wouldn't do this with plain old css? – excentris Commented Apr 30, 2013 at 13:30
7 Answers
Reset to default 6Add var
before $span
:
var $span = $(this).children('span');
Currently, you're declaring a global variable, which will be overwritten at each iteration in the loop.
Updated Demo
You have only one global $span
variable which after the loop will contain the last iterated element. Instead, make the variable local with a var
declaration:
$('.p').each(function() {
var $span = $(this).children('span');
$span.hover(
function() {
$span.css('color', 'red');
},
function() {
$span.css('color', 'blue');
}
)
});
There is no need for the .each()
You can try this:
$('.p').children('span').hover(
function() {
$(this).css('color', 'red');
},
function() {
$(this).css('color', 'blue');
});
check fiddle here
$("p span").hover(function(){
$("span ").css("color","red");
}).mouseout(function(){
$("p span ").css("color","black");
});
Please check here http://jsfiddle/VREtC/2/
$('.p').hover(
function() {
$(this).css('color', 'red');
},
function() {
$(this).css('color', 'blue');
}
)
If you want to make your code work, make $span
a closure variable by using var
$('.p').each(function() {
var $span = $(this).children('span');
$span.hover(
function() {
$span.css('color', 'red');
},
function() {
$span.css('color', 'blue');
}
)
});
Demo: Fiddle
try this
$('.p').each(function() {
var $span = $(this).children('span');
$span.hover(
function() {
$(this).css('color', 'red');
},
function() {
$(this).css('color', 'blue');
}
)
});
or without the loop (which is not required at all )
$('.p').children('span').hover(
function() {
$(this).css('color', 'red');
},
function() {
$(this).css('color', 'blue');
}
)
});
fiddle here
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742297673a4417450.html
评论列表(0条)