简体   繁体   English

单击时出现 jQuery 错误->未捕获的类型错误:无法读取未定义的属性“顶部”

[英]Getting jQuery error on click -> Uncaught TypeError: Cannot read property 'top' of undefined

I am not an jQuery expert and I would like to know if I am doing something wrong that generate errors in jQuery when clicking on 2 icons I have in a simple table.我不是 jQuery 专家,我想知道我是否做错了什么,在单击简单表格中的 2 个图标时会在 jQuery 中产生错误。

In the specific here when I click on the pdf icon or on the envelope icon I see in console that an error is generate (the number increase every time I click on one of the 2 icons).具体来说当我单击 pdf 图标或信封图标时,我在控制台中看到会生成错误(每次单击两个图标之一时,数字都会增加)。 Plus, the links I set for the icons are not working anymore.另外,我为图标设置的链接不再起作用。

On the right as you can see I have an animation.如您所见,在右侧我有一个 animation。 This is simply script code that I deactivated to see if was that the problem but it didn't worked.这只是我停用的脚本代码,以查看是否是问题所在,但没有奏效。

Unfortunately I am using a plugin for the CMS and I cannot change it's code.不幸的是,我正在使用 CMS 插件,我无法更改它的代码。

This is the error这是错误

(index):62 Uncaught TypeError: Cannot read property 'top' of undefined
    at HTMLTableElement.<anonymous> ((index):62)
    at HTMLTableElement.dispatch (jquery-1.12.4.js:5226)
    at HTMLTableElement.elemData.handle (jquery-1.12.4.js:4878)

This is the code这是代码

//your current click function
$('.scroll').on('click',function(e){
    e.preventDefault();
    $('html,body').animate({
        scrollTop:$($(this).attr('href')).offset().top + 'px'
    },1000,'swing');
});

You could try adding a class to the links for those two icons, eg I add class="clickable":您可以尝试将 class 添加到这两个图标的链接中,例如我添加 class="clickable":

<a class="clickable" href="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/C16-0.2.pdf" target="_blank"><img src="https://kirchmatt-breitenbach.ch/wp-content/uploads/2018/10/pdf.png" height="20" width="auto"></a>

Then add this to your javascript:然后将其添加到您的 javascript:

$(".clickable, .clickable img").on('click', function(e){
   e.stopPropagation();
}

So that the scroll javascript code doesn't run when you click on these two links.这样当您单击这两个链接时,滚动 javascript 代码不会运行。

Perhaps change the script:也许更改脚本:

//your current click function
$('.scroll').on('click',function(e){
    e.preventDefault();
    var href = $(this).attr('href'), $ele = $(href);
    if ($ele && $ele.offset()) {
      var top = $ele.offset().top + 'px'
      $('html,body').animate({
        scrollTop:top
      },1000,'swing');
    }   
});

暂无
暂无

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

相关问题 单击时弹出无法打开Uncaught TypeError:无法读取未定义的属性“ top” - popup on click not open Uncaught TypeError: Cannot read property 'top' of undefined 在JavaScript上获取“未捕获的TypeError:无法读取未定义的属性&#39;top&#39;” - Getting “Uncaught TypeError: Cannot read property 'top' of undefined” on javascript jQuery Uncaught TypeError:无法读取未定义的属性“ top” - jQuery Uncaught TypeError: Cannot read property 'top' of undefined 未捕获的TypeError:无法读取未定义的属性“ top”(jQuery) - Uncaught TypeError: Cannot read property 'top' of undefined (jquery) 图表 JS 错误:未捕获的类型错误:无法读取未定义的属性“顶部” - Chart JS Error : Uncaught TypeError: Cannot read property 'top' of undefined Uncaught TypeError:无法读取未定义的属性“ top”-滚动错误? - Uncaught TypeError: Cannot read property 'top' of undefined - Scrolling error? 控制台错误:未捕获TypeError:无法读取未定义的属性“顶部” - Console Error: Uncaught TypeError: Cannot read property 'top' of undefined 未捕获的TypeError:无法读取未定义错误的属性“ top” - Uncaught TypeError: Cannot read property 'top' of undefined error Javascript 错误未捕获类型错误:无法读取未定义的属性“顶部” - Javascript Error Uncaught TypeError: Cannot read property 'top' of undefined 错误:未被捕获的TypeError:无法读取未定义的属性“顶部” - Error: Uncaught TypeError: Cannot read property 'top' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM