Jquery addClass不工作,无法定义(此)
以下是我的javascript:Jquery addClass不工作,无法定义(此),jquery,this,addclass,Jquery,This,Addclass,以下是我的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 + '"></em
$(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");
});
});
$(函数(){
$(.image2”)。单击(函数(){var image=$(this.attr(“rel”);
$('#image2').hide();
$('#image2').fadeIn('slow');
$('#image2').html('');
返回false;
});
});
$(文档).ready(函数(){
$(“#拇指2 a”)。每个(函数(){
var image=$('#image2 embed').attr('src');alert(image);
if(this.attr('rel')=图像)
$(此).addClass(“打开”);
});
});
和我的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>
#拇指2 img{边框:1px实心#dfdfdf;填充:3px;高度:29px;边距顶部:5px;不透明度:0.3;}
#thumb2 img:hover{border:1px solid#fc7200;不透明度:1.0;}
.image2.open{边框:1px实心#fc7200;不透明度:1.0;}
无法将类添加到rel属性与embed src属性相同的链接。。。jquery已添加。。。请smb帮助 问题在于,
.attr()
不是DOM元素上可用的方法,=
设置(或尝试)左侧,而不是比较两者。要修复第一个问题,需要将其包装,因此:
if(this.attr('rel') = image )
需要:
if($(this).attr('rel') == image)
还请注意双精度==
用于比较,而不是一组
但是,一个更短、更高效的更好解决方案是:
$(document).ready(function() {
var image = $('#image2 embed').attr('src');
$("#thumb2 a[rel='" + image + "']").addClass("open");
});
@毒液,然后你可能想考虑上投票和/或接受答案=)Nick Craver,剩下4分钟)但是有一个问题…您知道如何将类添加到图像而不是链接a吗?@venom-是的,当然,只需执行
$(“#thumb2 a[rel=”“+image+”]img”).addClass(“打开”)代码>@Nick Craver,又是thx!但是)这是静态的,我的意思是,当我按下链接更改嵌入文件时,所选(打开的)img不会更改。。。我想一定是在以前的脚本中点击完成的@毒液-您想从另一个
中删除打开的?