code works in jsfiddle but not in html document

白昼怎懂夜的黑 提交于 2019-11-28 14:19:55

You want to put your code inside either

$(document).ready(function() {
    // Your code here
});

or

window.onload = function() {
    // Your code here
};

All of the elements need to be loaded first before the javascript executes. With jsFiddle you don't have to do this, but this is probably what's causing it not to work outside of jsFiddle.

You need to add this around your code$(document).ready(function(){   }); so your code runs after the page has loaded.

Otherwise the code will be looking for html that doesn't exist yet.

$(document).ready(function(){
$(targetElement).css({ left: '50px', top: '50px' }).animate({
        'left': '200px'  },{
       duration: speed,
        complete: function () {
            $(this).animate({
            'left':'50px'
            },{
            duration: speed,
            complete: function () {
         animateMe(this, speed);
                }
            })
        }
        }
   ); 
  };
animateMe($('#move'), 2000);

});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!