无法让 JSFiddle 工作
Cant get JSFiddle to Work
我将下面的代码添加到JSFiddle的顶部,但它停止工作,我认为这是一个简单的修复,但我无法弄清楚:
$('html, body').animate({
scrollTop: target.offset().top
}, 200);
// assign the correct target
var target = $('#if_two');
// scroll!
$('html, body').animate({
scrollTop: target.offset().top
}, 200, function(){
target.css({'border-color': 'red'})
});
我做错了什么?
问题只是代码的顺序。当代码被评估时,它实际上更像这样工作
var target;
$('html, body').animate({
scrollTop: target.offset().top
}, 200);
// assign the correct target
target = $('#if_two');
// scroll!
$('html, body').animate({
scrollTop: target.offset().top
}, 200, function(){
target.css({'border-color': 'red'})
});
这个过程称为可变吊装。所以当
$('html, body').animate({
scrollTop: target.offset().top
}, 200);
执行,target
仍未定义。如果您按以下方式对代码进行排序,它将起作用。
// assign the correct target
var target = $('#if_two');
$('html, body').animate({
scrollTop: target.offset().top
}, 200);
// scroll!
$('html, body').animate({
scrollTop: target.offset().top
}, 200, function(){
target.css({'border-color': 'red'})
});
这里是小提琴:http://jsfiddle.net/TomLee/TwtD9/5/
在定义目标之前,您正在使用目标。 有关工作示例,请参见此处:http://jsfiddle.net/TwtD9/4/
// assign the correct target
var target = $('#if_two');
$('html, body').animate({
scrollTop: target.offset().top
}, 200);
// scroll!
$('html, body').animate({
scrollTop: target.offset().top
}, 200, function(){
target.css({'border-color': 'red'})
});
所有的评论者和答案当然都是正确的,但是您知道开发人员控制台(大多数浏览器中的 f12)吗? 因为如果您打开该控制台,您将看到错误,如前所述:
'Uncaught TypeError: Cannot call method 'offset' of undefined'
相关文章:
- 代码只能在jsfiddle中工作
- JavaScript获胜't运行,但在jsFiddle中工作
- 为什么jquery悬停在jsfiddle中可以工作,而在我的html布局中却不能
- 我需要把jsFiddle的脚本放到工作中
- 代码在jsfiddle中工作,但在本地服务器中不起作用
- 尝试让AngularJS在JSFiddle中工作
- jQuery和其他从工作的jsfiddle复制到本地env时未运行的JS
- 在JSFiddle上工作,但不在本地工作
- 可以'我似乎无法让这个jsfiddle在本地工作.
- 为什么我的JSFiddle不工作?(放置脚本的位置)
- 正在jsfiddle.net上工作,但不在我的笔记本电脑上
- 为什么jQuery可以在jSfiddle中工作,但不能在站点中工作
- 当我尝试在jsFiddle中暂停并播放时,我的代码可以工作,但它不起作用;我的html页面不起作用
- 我如何才能让我的代码像在jsfiddle中一样工作
- jsFiddle工作,网站JS不工作
- 代码在 jsfiddle 中工作,但在我的网站上不起作用
- 我的jQuery在JSFiddle中工作,但不能在我的网站上工作!我做错了什么
- jQuery, bootstrap: input.val() 是未定义的,即使 jsfiddle 工作正常
- jQuery适用于JSFiddle,但不能在本地工作
- 代码工作 jsfiddle 不适用于 dreamweaver