jQuery/JS 错误当页面没有给定 ID 时
我遇到了一种情况,我很确定这是基本的情况,我只是不知道正确的语法/顺序:
我的网站的 $(document).ready 中有以下代码(函数():
$(document).ready(function() {
//Prevents default anchor tag behavior
$('#socialScroller a, #contributePod a, .prev, .next').click(function(e) {
e.preventDefault();
});
//Scrollable for Social Sidebar area
$(".socialScrollable").scrollable({ easing:"easeInOutCubic", vertical:true, next:".socialNext", prev:".socialPrev"});
var scrollable = jQuery(".socialScrollable").data("scrollable");
var size = 3;
scrollable.onSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
jQuery("a.socialNext").addClass("disabled");
}
});
scrollable.onBeforeSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
if (index > this.getIndex()) {
return false;
}
}
});
//Scrollable for History page
$(".historyScrollable").scrollable({ easing:"easeInOutCubic"}).navigator();
//Scrollables for Media page
$(".mediaScrollable").scrollable({ easing:"easeInOutCubic"}).navigator({navi:'#pressNavTabs'});
$("#mediaNavScrollable").scrollable({ easing:"easeInOutCubic", next:".nextMedia", prev:".prevMedia"});
var scrollable = jQuery("#mediaNavScrollable").data("scrollable");
var size = 4;
scrollable.onSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
jQuery("a.nextMedia").addClass("disabled");
}
});
scrollable.onBeforeSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
if (index > this.getIndex()) {
return false;
}
}
});
$("#mediaNavScrollable").scrollable({ easing:"easeInOutCubic"});
//History Scroller
$(function() {
$(".vertHistoryScroller").scrollable({ vertical:"true", easing:"easeInOutCubic", next:".nextVert", prev:".prevVert" }).navigator();
});
//Contribute Sidebar Actions
$(".contributeContainer").hide();
$("#contributePod a").click(function(){
var aData = $(this).attr("data-name");
$(".contributeContainer").fadeOut("fast");
$("#"+aData).fadeIn("slow");
});
$(".contributeContainer a").click(function(){
$(this).parent(".contributeContainer").fadeOut("fast");
});
});
在任何没有 #mediaNavScrollable 的页面上,我在 JS 控制台中收到此错误: 未捕获的类型错误:无法调用未定义的“onSeek”方法。
如果我从“varscrollable”行开始注释所有内容,则在没有 #mediaNavScrollable ID 的页面上一切正常。如何包装该 JS,使其仅在具有 #mediaNavScrollable 时触发?
I've got a situation, that I'm pretty sure is something basic, that I just don't know the proper syntax/sequence for:
I've got the following code in my site's $(document).ready(function()
:
$(document).ready(function() {
//Prevents default anchor tag behavior
$('#socialScroller a, #contributePod a, .prev, .next').click(function(e) {
e.preventDefault();
});
//Scrollable for Social Sidebar area
$(".socialScrollable").scrollable({ easing:"easeInOutCubic", vertical:true, next:".socialNext", prev:".socialPrev"});
var scrollable = jQuery(".socialScrollable").data("scrollable");
var size = 3;
scrollable.onSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
jQuery("a.socialNext").addClass("disabled");
}
});
scrollable.onBeforeSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
if (index > this.getIndex()) {
return false;
}
}
});
//Scrollable for History page
$(".historyScrollable").scrollable({ easing:"easeInOutCubic"}).navigator();
//Scrollables for Media page
$(".mediaScrollable").scrollable({ easing:"easeInOutCubic"}).navigator({navi:'#pressNavTabs'});
$("#mediaNavScrollable").scrollable({ easing:"easeInOutCubic", next:".nextMedia", prev:".prevMedia"});
var scrollable = jQuery("#mediaNavScrollable").data("scrollable");
var size = 4;
scrollable.onSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
jQuery("a.nextMedia").addClass("disabled");
}
});
scrollable.onBeforeSeek(function(event, index) {
if (this.getIndex() >= this.getSize() - size) {
if (index > this.getIndex()) {
return false;
}
}
});
$("#mediaNavScrollable").scrollable({ easing:"easeInOutCubic"});
//History Scroller
$(function() {
$(".vertHistoryScroller").scrollable({ vertical:"true", easing:"easeInOutCubic", next:".nextVert", prev:".prevVert" }).navigator();
});
//Contribute Sidebar Actions
$(".contributeContainer").hide();
$("#contributePod a").click(function(){
var aData = $(this).attr("data-name");
$(".contributeContainer").fadeOut("fast");
$("#"+aData).fadeIn("slow");
});
$(".contributeContainer a").click(function(){
$(this).parent(".contributeContainer").fadeOut("fast");
});
});
On any page that does not have #mediaNavScrollable, I get this error in the JS console:
Uncaught TypeError: Cannot call method 'onSeek' of undefined.
If I comment everything from the "var scrollable" line down, everything works fine on pages without that #mediaNavScrollable ID. How do I wrap that JS so that it only fires if it has #mediaNavScrollable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要检查
scrollable
是否不为 null 并且其中包含任何元素。如果没有,则退出处理程序。这将退出处理程序而不运行任何其他代码。或者,您可以使用 if 块:
You need to check to see if
scrollable
is not null and has any elements in it. If it doesnt, exit the handler.This will exit the handler without running any other code. Alternately, you can use an if block:
您需要检查是否存在可滚动:
You need to check for the existence of scrollable:
您可以先检查该元素是否存在,如下所示:
You can check to see if the element exists first, like this:
这不是由缺少元素引起的。如果缺少该元素,您将永远不会将其放入发生错误的代码中。编辑 — 哎呀,我没有将操作代码向右滚动得足够远;我认为下面令人困惑的缩进代码是处理程序函数的一部分。相反,问题在于:
没有返回任何内容。 “mediNavScrollable”元素上没有存储名为“scrollable”的数据元素。这可能意味着 HTML 中的元素应该如下所示:
但我对此表示怀疑,因为该代码似乎期望它是一个 DOM 元素。 (这也是 jQuery 和旧式 DOM 0 处理程序的令人不快的混合)。这意味着要使代码正常工作,您不仅需要该元素位于页面上,而且还必须在元素的 jQuery 数据存储中保留对 DOM 节点的引用。
That's not being caused by a missing element. If the element were missing, you'd never make it into the code where the error happens.edit — oops I hadn't scrolled the OP's code far enough to the right; I thought the code below it that's confusingly indented was part of a handler function.Instead, the problem is that this:
is returning nothing. There's no data element called "scrollable" stored on your "mediNavScrollable" element. That may mean that the element in your HTML is supposed to look like this:
but I sort of doubt it, as that code seems to expect it to be a DOM element. (Sort of an unpleasant mix of jQuery and old-style DOM 0 handlers, too). That means that for the code to work, not only do you need that element to be on the page, but something will have had to stick a reference to a DOM node in the jQuery data store for the element.