简体   繁体   English

单击元素时,在可滚动div中滚动内容

[英]Scroll the content inside a scrollable div when clicking an element

I have a DIV which is limited in height, and has a scrollable overflow. 我有一个高度受限制的DIV,并且具有可滚动的溢出。 I would like to add two manual scroll links that when they are clicked, the content inside the DIV scrolls up or down. 我想添加两个手动滚动链接,当单击它们时,DIV中的内容将向上或向下滚动。

CSS CSS

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

JS JS

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

HTML 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>

However it does not really work ... 但是,它实际上并没有工作...

You can't use CSS selector in getElementById . 您不能在getElementById使用CSS选择器。

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

You used a double selector, getElementById does not need the # in its argument. 您使用了双选择器, getElementById的参数中不需要# And if you put the javascript between the <head> tags it should work fine. 如果将javascript放在<head>标记之间,它应该可以正常工作。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM