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');
    }

}
4

5 回答 5

3

我认为使用 Jquery 的正确方法是:

$('div.video').addClass('active');
于 2012-05-01T09:12:32.713 回答
3

如果 url 确实有hash ,则具有匹配类的 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");
}
于 2012-05-01T09:27:11.947 回答
1

我不确定,但您是否正在寻找这个: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');
    }
}
于 2012-05-01T09:13:15.167 回答
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');
}
于 2012-05-01T09:21:55.780 回答
-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");
    });
});
}
于 2012-05-01T09:13:39.267 回答