jQuery/JS 错误当页面没有给定 ID 时

发布于 2024-12-05 11:50:38 字数 2469 浏览 1 评论 0原文

我遇到了一种情况,我很确定这是基本的情况,我只是不知道正确的语法/顺序:

我的网站的 $(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南烟 2024-12-12 11:50:38

您需要检查 scrollable 是否不为 null 并且其中包含任何元素。如果没有,则退出处理程序。

var scrollable = jQuery("#mediaNavScrollable").data("scrollable");
if(scrollable == null || scrollable.length==0) return;

这将退出处理程序而不运行任何其他代码。或者,您可以使用 if 块:

var scrollable = jQuery("#mediaNavScrollable").data("scrollable");
if(scrollable == null || scrollable.length==0) 
{
    //element doesnt exist, perform alternate action
}
else
{
    //element does exist, do normal stuff here
    scrollable.onSeek(......);
}

You need to check to see if scrollable is not null and has any elements in it. If it doesnt, exit the handler.

var scrollable = jQuery("#mediaNavScrollable").data("scrollable");
if(scrollable == null || scrollable.length==0) return;

This will exit the handler without running any other code. Alternately, you can use an if block:

var scrollable = jQuery("#mediaNavScrollable").data("scrollable");
if(scrollable == null || scrollable.length==0) 
{
    //element doesnt exist, perform alternate action
}
else
{
    //element does exist, do normal stuff here
    scrollable.onSeek(......);
}
沦落红尘 2024-12-12 11:50:38
if ($("#mediaNavScrollable").length){
      // your code
}
if ($("#mediaNavScrollable").length){
      // your code
}
萌梦深 2024-12-12 11:50:38

您需要检查是否存在可滚动:

if(scrollable)
{ 
  ...
}

You need to check for the existence of scrollable:

if(scrollable)
{ 
  ...
}
清引 2024-12-12 11:50:38

您可以先检查该元素是否存在,如下所示:

if ($("#mediaNavScrollable").length) {
  //your code here
}

You can check to see if the element exists first, like this:

if ($("#mediaNavScrollable").length) {
  //your code here
}
饭团 2024-12-12 11:50:38

这不是由缺少元素引起的。如果缺少该元素,您将永远不会将其放入发生错误的代码中。 编辑 — 哎呀,我没有将操作代码向右滚动得足够远;我认为下面令人困惑的缩进代码是处理程序函数的一部分。

相反,问题在于:

var scrollable = jQuery("#mediaNavScrollable").data("scrollable");

没有返回任何内容。 “mediNavScrollable”元素上没有存储名为“scrollable”的数据元素。这可能意味着 HTML 中的元素应该如下所示:

<div id='mediaNavScrollable' data-scrollable='something'>

但我对此表示怀疑,因为该代码似乎期望它是一个 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:

var scrollable = jQuery("#mediaNavScrollable").data("scrollable");

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:

<div id='mediaNavScrollable' data-scrollable='something'>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文