Javascript 展开后跳转到div的顶部
我有以下代码Javascript 展开后跳转到div的顶部,javascript,jquery,Javascript,Jquery,我有以下代码 <ul id="menu"> <li class="newsPara"> Initial copy here <a href="#" class="readmore">Read more» </a> <ul class="accordianUl" style="display: none"> <li>
<ul id="menu">
<li class="newsPara">
Initial copy here
<a href="#" class="readmore">Read more» </a>
<ul class="accordianUl" style="display: none">
<li>
content hidden here
</li>
</ul>
</li>
</ul>
$('#menu li a').click(function () {
$('#menu li a').show();
$(this).hide();
var checkElement = $(this).next('ul');
if (checkElement.is(':visible')) {
return false;
}
if (!checkElement.is(':visible')) {
$('#menu ul:visible').slideUp('normal');
checkElement.slideDown('normal');
$('html, body').animate({ scrollTop: $(this).parent('.accordianUl').position().top }, 'slow');
return false;
}
});
我想一旦sub ul扩展到它的顶端,我就试过了
$('html, body').animate({ scrollTop: $(this).parent('.accordianUl').position().top }, 'slow');
但它似乎不起作用
有人能帮忙吗
谢谢你的$(this)。parent()
返回空值,因为它没有相应的代码$(this)指调用函数的当前元素,请参见此提琴:
嘿,你能提供比“那没用”更详细的信息吗?在小提琴中工作很好:你可以在这里看到我在那页上丢失的页面,而不必费心在上面寻找你的问题。lol.努力是相对的兄弟。
$('#menu li a').click(function () {
$('#menu li a').show();
$(this).hide();
$this = $(this); //KEEP THAT ELEMENT JUST INCASE
console.log($this); //IT IS THE LI > A
var checkElement = $(this).next('ul');
if (checkElement.is(':visible')) {
return false;
}
if (!checkElement.is(':visible')) {
$('#menu ul:visible').slideUp('normal');
checkElement.slideDown('normal');
$('html, body').animate({ scrollTop: checkElement.position().top }, 'slow');
return false;
}
});