addClass doesnt work, cant define (this)
here is my javascript:
$(function() {
$(".image2").click(function() {var image = $(this).attr("rel");
$('#image2').hide();
$('#image2').fadeIn('slow');
$('#image2').html('<embed height="253" width="440" wmode="transparent" src="' + image + '"></embed>');
return false;
});
});
$(document).ready(function() {
$("#thumb2 a").each(function() {
var image = $('#image2 embed').attr('src');alert(image);
if(this.attr('rel') = image )
$(this).addClass("open");
});
});
and my html:
<style>
#thumb2 img { border:1px solid #dfdfdf; padding:3px; height:29px; margin-top:5px; opacity:0.3; }
#thumb2 img:hover { border:1px solid #fc7200; opacity:1.0; }
.image2.open { border:1px solid #fc7200; opacity:1.0; }
</style>
<div id="image2"><embed height="253" width="440" wmode="transparent" src="images/kameralar.swf"></embed></div>
<div id="thumb2">
<a href="#" rel="images/kameralar.swf" class="image2" ><img src="images/t1.png" border="0"/></a>
<a href=开发者_开发百科"#" rel="images/standalone.swf" class="image2"><img src="images/t2.png" border="0"/></a>
<a href="#" rel="images/mobil.swf" class="image2"><img src="images/t3.png" border="0"/></a>
<a href="#" rel="images/3b2.swf" class="image2"><img src="images/t4.png" border="0"/></a>
<a href="#" rel="images/canon.swf" class="image2"><img src="images/t5.png" border="0"/></a>
<a href="#" rel="images/indigovision.swf" class="image2"><img src="images/t6.png" border="0"/></a>
</div>
can't add class to a link whose rel attribute is the same with embed src attribute... jquery is added... pls smb help!
The problem is that .attr()
isn't a method available on the DOM element and =
sets (or attempts to) the left side, rather than comparing both. To fix the first, you need to wrap it, so this:
if(this.attr('rel') = image )
needs to be:
if($(this).attr('rel') == image)
Also note the double ==
for a comparison, rather than a set.
However, a better solution that both shorter and more efficient would be:
$(document).ready(function() {
var image = $('#image2 embed').attr('src');
$("#thumb2 a[rel='" + image + "']").addClass("open");
});
精彩评论