【发布时间】:2014-07-23 23:37:15
【问题描述】:
我正在用 JS 编写代码,但我不知道单击按钮时如何填充数组。我们有这段代码,它使用一个列表(ul),其中项目(li)可以用鼠标移动。 onclick如何用2个数据填充一个数组,它的第一个位置和最后一个位置?
<!doctype html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="https://raw.github.com/furf/jquery-ui-touch-punch/master/jquery.ui.touch-punch.min.js"></script>
<script>
$(function() {
$( ".documents" ).sortable();
$( ".documents" ).disableSelection();
});
</script>
<meta charset="utf-8">
<title>toArray demo</title>
<style>
span {
color: red;
}
</style>
</head>
<body>
Reversed - <span></span>
<ul id="opciones" class="documents">
<li>uno</li>
<li>dos</li>
<li>tres</li>
</ul>
<script>
function disp( li ) {
var a = [];
for ( var i = 0; i < li.length; i++ ) {
a.push( li[ i ].innerHTML );
}
$( "span" ).text( a.join( " " ) );
}
disp( $( "li" ).toArray() );
</script>
<input type="button" value="actualizar_array" onclick="disp('#opciones')" />
</body>
</html>
【问题讨论】:
标签: javascript jquery html ajax arrays