[英]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&fieldname=shipfname&fieldvalue=' + this.value + '&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.