单击元素时滚动可滚动 div 内的内容

Scroll the content inside a scrollable div when clicking an element

我有一个 DIV,它的高度有限,并且有一个可滚动的溢出。我想添加两个手动滚动链接,当它们被点击时,DIV 中的内容向上或向下滚动。

CSS

#inner {
   max-height: 400px;
   overflow: scroll;
}

JS

function scrolldown() {
   document.getElementById('#inner').scrollTop -= 10;
}
function scrollup() {
   document.getElementById('#inner').scrollTop += 10;
}

HTML

<div id="inner">
    Looooong content here
</div>
<a href="#" onClick="return false" onmousedown="javascript:scrollup();">
    <img src="up.png"/>
</a>
<a href="#" onClick="return false" onmousedown="javascript:scrolldown();">
    <img src="down.png"/>
</a>

然而它并没有真正起作用...

您不能在 getElementById 中使用 CSS 选择器。

function scrolldown() {
    document.getElementById('inner').scrollTop -= 10;
}
function scrollup() {
    document.getElementById('inner').scrollTop += 10;
}

您使用了双重选择器,getElementById 的参数中不需要 #。如果您将 javascript 放在 <head> 标签之间,它应该可以正常工作。

function scrolldown() {
    document.getElementById('inner').scrollTop -= 10;
}
function scrollup() {
    document.getElementById('inner').scrollTop += 10;
}