简体   繁体   English

jQuery-为什么在document.ready上调用我的函数时会中断?

[英]jQuery - Why does my function break if called on document.ready?

I'm doing some HTML prototyping to explore some UX stuff. 我正在做一些HTML原型研究一些UX的东西。 Whilst trying to work out why a Javascript function (which uses a lot of jQuery) wasn't working I broke it out into a JS Fiddle. 在试图弄清为什么Javascript函数(使用大量jQuery)无法正常工作的同时,我将其分解为JS Fiddle。 It seems that when the function is called on document.ready it won't work and can't be executed with a click event. 似乎在document.ready上调用该函数时,该函数将不起作用,并且无法与click事件一起执行。 However, if it isn't called at document.ready then it will work! 但是,如果未在document.ready中调用它,那么它将起作用! I'm probably missing something obvious... 我可能缺少明显的东西...

Working: http://jsfiddle.net/NWmB5/ 工作: http //jsfiddle.net/NWmB5/

$(document).ready(function() {
    $('#targetFirstDiv').click(function() {
        setTimelinePosition($('#anotherDiv'));  
    });
    $('#targetSecondDiv').click(function() {
        setTimelinePosition($('#testDiv'));
    });   
});
var toDoCategories = [$("#testDiv"),$("#anotherDiv")];
/* Show current position on timeline */
function setTimelinePosition($position) {
    var $theTimelineTrigger = $('span.timelineTrigger');
        toDoCategories.forEach(function(currentCategory) {
            var $deselectTimelinePositionElement = $(currentCategory, $theTimelineTrigger);
            $($deselectTimelinePositionElement).removeClass('currentPosition');
         });
        var $selectTimelinePositionElement = ($($position,$theTimelineTrigger));
        $($selectTimelinePositionElement).addClass('currentPosition');
    }

Not Working: http://jsfiddle.net/NWmB5/5/ 不起作用: http : //jsfiddle.net/NWmB5/5/

$(document).ready(function() {
    setTimelinePosition($('#thirdDiv'));
    $('#targetFirstDiv').click(function() {
        setTimelinePosition($('#anotherDiv'));
    });
    $('#targetSecondDiv').click(function() {
        setTimelinePosition($('#testDiv'));
    });

});
var toDoCategories = [$("#testDiv"),$("#anotherDiv"),$("thirdDiv")];

/* Show current position on timeline */
function setTimelinePosition($position) {
    var $theTimelineTrigger = $('span.timelineTrigger');
    toDoCategories.forEach(function(currentCategory) {
        var $deselectTimelinePositionElement = $(currentCategory, $theTimelineTrigger);
        $($deselectTimelinePositionElement).removeClass('currentPosition');
    });
    var $selectTimelinePositionElement = ($($position,$theTimelineTrigger));
    $($selectTimelinePositionElement).addClass('currentPosition');
}

You trying to get $("#testDiv"),$("#anotherDiv"),$("thirdDiv") before DOM ready event fired that's where the exact problem.Try after DOM ready fired 您尝试在触发DOM就绪事件之前获取$(“#testDiv”),$(“#anotherDiv”),$(“ thirdDiv”),这就是确切的问题所在。尝试在DOM ready触发后尝试

 var toDoCategories = [$("#testDiv"),$("#anotherDiv"),$("thirdDiv")]; 

So get $("#testDiv"),$("#anotherDiv"),$("thirdDiv") after DOM ready event dispatched. 因此,在分派DOM ready事件后,获取$(“#testDiv”),$(“#anotherDiv”),$(“ thirdDiv”)。

 var toDoCategories; //NOTE HERE

$(document).ready(function() {

  toDoCategories = [$("#testDiv"),$("#anotherDiv"),$("thirdDiv")]; //NOTE HERE

     setTimelinePosition($('#thirdDiv'));

$('#targetFirstDiv').click(function() {
     setTimelinePosition($('#anotherDiv'));

});
$('#targetSecondDiv').click(function() {
     setTimelinePosition($('#testDiv'));

});

});


/* Show current position on timeline */
function setTimelinePosition($position) {


          var $theTimelineTrigger = $('span.timelineTrigger');

  toDoCategories.forEach(function(currentCategory) {


var $deselectTimelinePositionElement = $(currentCategory, $theTimelineTrigger);

          $($deselectTimelinePositionElement).removeClass('currentPosition');
     });

  var $selectTimelinePositionElement = ($($position,$theTimelineTrigger));
  $($selectTimelinePositionElement).addClass('currentPosition');


   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM