[英]Fading in element with the same id
I am having troubles with this code. 我在使用此代码时遇到麻烦。 It's supposed to do this:
应该这样做:
Parenting: #portfolioHolder (visible at the start of the function) .portfolioImage #details (invisible at the start of the function) .image 育儿:#portfolioHolder(在函数开始时可见).portfolioImage #details(在函数开始时不可见).image
My Code: 我的代码:
$(function(){
$("div.portfolioImage").click(function(){
var id = $(this).id;
window.print($(this).id);
$("div#portfolioHolder").fadeOut('slow', function() {
// Animation complete.
});
$("div#details").fadeIn('slow', function() {
// Animation complete.
});
$("div.image").each.fadeOut('slow', function(){/*complete*/});
$("div.image#"+id.toString()).fadeIn('slow', function(){/*complete*/});
});
}); });
-- David - 大卫
You have couple of issues in your code. 您的代码中有几个问题。
this.id
or $(this).attr('id')
this.id
或$(this).attr('id')
获取ID $("div.image").each.fadeOut
will throw an error because each
is a method and you are trying to use it as a property and calling fadeOut
method on it. $("div.image").each.fadeOut
将引发错误,因为each
方法都是一种方法,并且您试图将其用作属性并在其上调用fadeOut
方法。 You don't have to use each
here because $("div.image").fadeOut()
will take care of running fadeOut
on all the selected elements. each
,因为$("div.image").fadeOut()
将照顾在所有选定元素上运行fadeOut
情况。 Try this 尝试这个
$(function(){
$("div.portfolioImage").click(function(){
var id = this.id;
$("div#portfolioHolder").fadeOut('slow', function() {
// Animation complete.
});
$("div#details").fadeIn('slow', function() {
// Animation complete.
});
$("div.image").fadeOut('slow', function(){/*complete*/});
$("div.image#"+id).fadeIn('slow', function(){/*complete*/});
});
});
IDs should be unique! ID应该是唯一的! But you can use data-id or any data-X instead...
但是您可以使用data-id或任何data-X代替...
Instead of: 代替:
<div class="portfolioImage" id="3">...</div>
<div class="image" id="3">...</div>
use: 采用:
<div class="portfolioImage" id="image3">...</div>
<div class="image" data-id="image3">...</div>
and the JS: 和JS:
$('.portfolioImage').click(function(){
var id = $(this).attr('id');
$('.image').fadeOut('slow');
$('.image[data-id='+id+']').fadeIn('slow');
$('#portfolioHolder').fadeOut('slow');
$('.details').fadeIn('slow');
});
should be right 应该是对的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.