如何创建一个滚动框然后用javascript添加东西?

时间:2022-01-04 15:49:17

I'm making a game and i want to create a scrollbox with html, and then add things to it later with javascript, further on in the same code. Is that possible? Thanks.

我正在制作一个游戏,我想用html创建一个滚动条,然后用javascript添加内容,进一步在同一个代码中。那可能吗?谢谢。

2 个解决方案

#1


2  

Below is an example done with Javascript, you should be able to go from there, though I would recommend doing this with jQuery instead, since manipulating DOM objects will be much easier.

下面是用Javascript完成的一个例子,你应该可以从那里开始,虽然我建议用jQuery来做这个,因为操作DOM对象会容易得多。

Here's the fiddle: http://jsfiddle.net/g9Tys/

这是小提琴:http://jsfiddle.net/g9Tys/

CSS (for scrollbox):

CSS(用于滚动框):

#scrollbox {
overflow: scroll;
width: 600px;
height: 100px;
}

A scrollbox and link to add new elements:

用于添加新元素的滚动框和链接:

<div id="scrollbox">
</div>
<a onclick="addElement()">Add element</a>

Javascript:

使用Javascript:

function addElement() {
    var scrollbox = document.getElementById('scrollbox');

    // Create some element, e.g. div
    var newElement = document.createElement('div');
    newElement.setAttribute('id', "some-id-for-new-element");
    newElement.innerHTML = 'New element has been added!';

    scrollbox.appendChild(newElement);
}

#2


0  

Yes it is possible. you should use div them make it overflow

对的,这是可能的。你应该使用div让它溢出

div

DIV

{

{

width:150px;

宽度:150像素;

height:150px;

高度:150像素;

word-wrap:break-word;

自动换行:打破字;

overflow:scroll;

溢出:滚动;

}

}

something like that.

类似的东西。

then use javascript and append() event

然后使用javascript和append()事件

#1


2  

Below is an example done with Javascript, you should be able to go from there, though I would recommend doing this with jQuery instead, since manipulating DOM objects will be much easier.

下面是用Javascript完成的一个例子,你应该可以从那里开始,虽然我建议用jQuery来做这个,因为操作DOM对象会容易得多。

Here's the fiddle: http://jsfiddle.net/g9Tys/

这是小提琴:http://jsfiddle.net/g9Tys/

CSS (for scrollbox):

CSS(用于滚动框):

#scrollbox {
overflow: scroll;
width: 600px;
height: 100px;
}

A scrollbox and link to add new elements:

用于添加新元素的滚动框和链接:

<div id="scrollbox">
</div>
<a onclick="addElement()">Add element</a>

Javascript:

使用Javascript:

function addElement() {
    var scrollbox = document.getElementById('scrollbox');

    // Create some element, e.g. div
    var newElement = document.createElement('div');
    newElement.setAttribute('id', "some-id-for-new-element");
    newElement.innerHTML = 'New element has been added!';

    scrollbox.appendChild(newElement);
}

#2


0  

Yes it is possible. you should use div them make it overflow

对的,这是可能的。你应该使用div让它溢出

div

DIV

{

{

width:150px;

宽度:150像素;

height:150px;

高度:150像素;

word-wrap:break-word;

自动换行:打破字;

overflow:scroll;

溢出:滚动;

}

}

something like that.

类似的东西。

then use javascript and append() event

然后使用javascript和append()事件