Problem with each()
<script type="text/javascript">
//<!--
$(document).ready(function() {
$('ul.course-nav li a').each(function() {
alert(5);
if ('#' == $(this).attr('href')) {
$(this).addClass('lessOpacity');
}
});
}); //-->
</script>
HTML of course contains searched elements:
<ul class="course-nav">
<li><a href="navigator.php?kam=zakladnyNahladKurzu&id=1&pos=2" class="next"><img src="css/images/16_arrow_right.png" alt="next"></a></li>
<li><a href="#" class="prev"><img src="css/images/16_arrow_left.png" alt="prev"></a></li>
<li><a href="navigator.php?kam=zakladnyNahladKurzu&id=1" class="start"><img src="css/images/16_arrow_first.png" alt="start"></a></li>
</ul>
Yet it does开发者_如何学运维n't work. Not even alerts pop up. Any suggestions?
You don't need each
there, you can simply do:
$('ul.course-nav a[href="#"]').addClass('lessOpacity');
The jQuery's implicit iteration will look through all links whose href
is set to #
and add the class accordingly. Make sure to wrap your code in ready handler:
$(function(){
$('ul.course-nav a[href="#"]').addClass('lessOpacity');
});
intead of
$('ul.course-nav a').each(function() {
alert(5);
if ('#' == $(this).attr('href')) {
$(this).addClass('lessOpacity');
}
});
replace with
$('ul.course-nav li a').each(function() {
alert(5);
if ('#' == $(this).attr('href')) {
$(this).addClass('lessOpacity');
}
});
Wrap your function with
$(document).ready(function() {
// here goes your function
});
The whole page has to be loaded until jQuery can modify anything!
Perhaps another $(document).ready() function on the page is executed instead of the presented one.
精彩评论