2016-06-25 4 views
1

아래 코드는 Chrome에서 정상적으로 작동하지만 Safari에서 다음과 같은 오류가 발생합니다. 그것을 해결할 수있는 해결책이 있습니까?TypeError : undefined가 Safari 및 iOS에서만 사용되는 객체가 아닙니다.

jQuery('.mk-responsive-nav > li > a').click(function() { 
    var href = jQuery(this).attr('href').replace('#', ''); 
    jQuery(window).scrollTop(jQuery("section[data-anchor='" + href + "']").offset().top); 
    console.log(jQuery("section[data-anchor='" + href + "']").offset().top ); 
}); 

사파리 오류 :

오류 : 미정은 (평가 대상 아니다 'jQuery를 ("절 [데이터 앵커 ='"+ HREF가 + ''] "..) (오프셋) 위쪽 ')

답변

0

컨텍스트 html을 볼 수 없기 때문에 말하기가 어렵습니다. href 변수를 트랩하고 window을 스크롤하기 전에 실제로는 jQuery Object인지 확인하는 것이 좋습니다. 나는 당신이 가진 것과 같은 오류를 가져올 수 있었다.

$(function() { 
 
    jQuery('.mk-responsive-nav > li > a').click(function() { 
 

 

 
    var href = jQuery(this).attr('href').replace('#', ''); 
 
    var target = jQuery("section[data-anchor='" + href + "']"); 
 

 
    console.log(target.offset().top, 'YOUR EXACT ERROR IN SAFARI WHEN CLICKING FAIL LINK: ', jQuery("section[data-anchor='" + href + "']")); 
 

 
    if (target.length) { 
 
     console.log('scrolling to: ', target.offset().top); 
 
     jQuery(window).scrollTop(target.offset().top); 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul class="mk-responsive-nav"> 
 
    <li><a href="#section-1">This will work</a> 
 
    </li> 
 
    <li><a href="#section-fail">This will fail</a> 
 
    </li> 
 
</ul> 
 

 
<section data-anchor="section-1">Section 1.</section> 
 
<section data-anchor="section-2">Section 2.</section>

관련 문제