2012-05-01 67 views
2

如何选择在jQuery中传递if语句的dom元素?

我正在检查网址是否有散列。哈希匹配视频容器的类。如果url确实有一个散列,则带有匹配类的iframe会获得一个活动类,以将其显示为块而不是隐藏。如果没有,则显示在列表中的第一个视频:

jQuery(document).ready(function($) { 

    videoCarousel(); 

}); 

function videoCarousel() { 

    // Set variables 
    var carousel = $('#video_container'); 
    var videos = $('#video_container #video iframe'); 

    // Get url hash and remove # from begining 
    var hash = window.location.hash.substring(1); 


    if (videos.hasClass(hash)) { 
     $(this).addClass('active'); 
    } else { 
     videos.first().addClass('active'); 
    } 

} 
+2

这是不可能告诉没有更多的上下文是否是正确的代码或错误代码。请张贴更多的脚本,以及一些HTML。 –

+0

if语句是正确的,我认为问题出在你的'$(this)'上。通过'$(this)'如果你的意思是$('div')',它将不起作用。你将不得不使用'$('div')'。 –

+0

你真的不需要'if'。看看@ Neysor和@ JNF的回答 –

回答

3

如果url确实有哈希,则iframe 与匹配类获取一个活动类,将其显示为块,而不是隐藏。如果没有,则显示在列表中的第一个视频:

(我的重点)

这是关键信息,我想我们很多人看过去吧。你需要改变你的选择,你不想this都:

function videoCarousel() { 
    // Set variables 
    var carousel = $('#video_container'); 
    var videos = $('#video_container #video iframe'); 

    // Get url hash and remove # from begining 
    var hash = window.location.hash.substring(1); 

    // Get the video with the matching class 
    var targetVideo; 
    if (hash) { 
     targetVideo = videos.filter("." + hash); 
    } 
    if (!targetVideo || !targetVideo[0]) { 
     // There's no hash, or no match; use the first 
     targetVideo = videos.first(); 
    } 

    // Add the class 
    targetVideo.addClass("active"); 
} 
3

我想使用jQuery将是正确的做法:

$('div.video').addClass('active'); 
+3

@ MerlynMorgan-Graham:*“如果没有该类的div,这将会引发空引用异常......”*否,不会。这是关于jQuery的一件大事,它是基于集合的。所以如果没有'div.video',你会得到一个空的jQuery对象(一个空集),而不是'null'。你可以在空的jQuery对象上调用'addClass'。这完全没问题,它最终成为一个没有操作。 –

+0

@ MerlynMorgan-Graham:不会。jQuery自v1.0版本开始就基于set:http://jsbin.com/azumaw –

+0

@ T.J.Crowder:Aha。我刚刚发现我一直在使用一些旧的毛发版本的原型,我只是认为它是jQuery的一些旧版本。感谢您挑战我的假设。 –

0

我不知道,但你找这样的:DEMO

$("div.video").each(function(){ 
    $(this).addClass("active"); 
}); 

,因为你的编辑

认为你可以想有:

jQuery(document).ready(function($) { 

    videoCarousel(); 

}); 

function videoCarousel() { 

    // Set variables 
    var carousel = $('#video_container'); 
    var videos = $('#video_container #video iframe'); 

    // Get url hash and remove # from begining 
    var hash = window.location.hash.substring(1); 

    var hasone = false; 
    videos.each(function(){ 
     if($(this).hasClass(hash)) { 
     $(this).addclass("active"); 
     hasone = true; 
     return false; //breaks each(); 
     } 
    }); 
    if(!hasone) { 
     videos.first().addClass('active'); 
    } 
} 
+0

'div'和'.video'之间不应有空格。小提琴是正确的。 – Nix

+0

@nix谢谢,纠正它! – Neysor

-1

也许尝试这样的:

function videoCarousel() { 

// Set variables 
var carousel = $('#video_container'); 
var videos = $('#video_container #video iframe'); 

// Get url hash and remove # from begining 
var hash = window.location.hash.substring(1); 


$(".yourClass").each(function() { 
    if ($(this).hasClass(hash)) { 
     $(this).addClass("active"); 
    } else { 
     videos.first().addClass("active"); 
    }); 
}); 
} 
0
function videoCarousel() { 

    // Set variables 
    var carousel = $('#video_container'); 
    var videos = $('#video_container #video iframe'); 

    // Get url hash and remove # from begining 
    var hash = window.location.hash.substring(1); 
    //Filter out the element that match the hash or is the first video in the collection 
    var activeVideo = videos.filter(function(i) { 
     return hash === "" ? i === 0 : $(this).hasClass(hash); 
    }); 

    activeVideo.addClass('active'); 
}