简体   繁体   English

未捕获的TypeError:无法读取未定义的属性“ top”

[英]Uncaught TypeError: Cannot read property 'top' of undefined

Trying to use a button to go to an anchor. 尝试使用按钮转到锚点。

My JS code is: 我的JS代码是:

    function scrollToAnchor(aid){
        var aTag = $("a[name='"+ aid +"']");
        $('html,body').animate({scrollTop: aTag.offset().top},'slow');
    }

    $("#AsGuest").click(function() {
       scrollToAnchor('FName_Ship');
    });

HTML button I'm using as the click: 我用作点击的HTML按钮:

 <input type="Submit" id="AsGuest" class="btn btn-orange" value="CONTINUE AS A GUEST" style="margin-top:5px;"/>

Area I'm going to upon click: 我要单击的区域:

 <input type="text" id="FName_Ship" name="FName_Ship" value="James" onchange="$.ajax( { type: 'GET', dataType: 'json', url: 'dmiajaxsecure.aspx?request=updatedata&amp;fieldname=shipfname&amp;fieldvalue=' + this.value + '&amp;extra=' + rnd() } );" class="valid">

jQuery.offset() returns null when you it is called on an empty jQuery object. 当您在空的jQuery对象上调用jQuery.offset()时,它会返回null Check aTag.length before attempting to call .offset() method, most likely the anchor does not exist. 在尝试调用.offset()方法之前检查aTag.length ,很可能是锚点不存在。

Replace the a tag by input , as it is the element you actually target. 更换a由标签input ,因为它是你真正的目标元素。

function scrollToAnchor(aid){
    var inputTag = $("input[name='"+ aid +"']");
    $('html,body').animate({scrollTop: inputTag.offset().top},'slow');
}

$("#AsGuest").click(function() {
   scrollToAnchor('FName_Ship');
});

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

相关问题 未捕获的类型错误:无法读取未定义的属性“顶部” - Uncaught TypeError: Cannot read property ‘top’ of undefined 未捕获的TypeError:无法读取未定义的属性“ top” - Uncaught TypeError: Cannot read property 'top' of undefined 未捕获的类型错误:无法读取未定义的属性“顶部” - Uncaught TypeError: Cannot read property 'top' of undefined 未捕获的TypeError:无法读取未定义的属性“ top” - Uncaught TypeError: Cannot read property 'top' of undefined 购物车飞未捕获的TypeError:无法读取未定义的属性“顶部” - Cart to Fly Uncaught TypeError: Cannot read property 'top' of undefined 图表 JS 错误:未捕获的类型错误:无法读取未定义的属性“顶部” - Chart JS Error : Uncaught TypeError: Cannot read property 'top' of undefined jQuery Uncaught TypeError:无法读取未定义的属性“ top” - jQuery Uncaught TypeError: Cannot read property 'top' of undefined Uncaught TypeError:无法读取未定义的属性“ top”-滚动错误? - Uncaught TypeError: Cannot read property 'top' of undefined - Scrolling error? 未捕获的TypeError:无法读取未定义的属性“ top”(jQuery) - Uncaught TypeError: Cannot read property 'top' of undefined (jquery) 控制台错误:未捕获TypeError:无法读取未定义的属性“顶部” - Console Error: Uncaught TypeError: Cannot read property 'top' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM