我正在研究我的代码,想弄清楚如何将可拖动对象存储到数组中,因为我正在创建一个数学游戏,该游戏将识别可拖动元素并确定问题是否正确,这是我的代码
<script>
$(function() {
$( "#draggable1" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable2" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable3" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable4" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable5" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable6" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable7" ).draggable();
});
</script>
<script>
$(function() {
$( "#draggable8" ).draggable();
});
</script>
然后我试图将这些可拖动元素放入一个数组中
<div data-role="main" class="ui-content">
<script>
var theimagestwo = new Array();
theimagestwo[1] = "#draggable1";
theimagestwo[2] = "#draggable2";
document.getElementById("pagethree").innerHTML = theimagestwo[1];
</script>
我做错了什么?他们只是以文本形式出现
最佳答案
看看你的这部分代码(我添加了注释):
// Create a new array
var theimagestwo = new Array();
// Push the strings "#draggable1" and "#draggable2" into the array
theimagestwo[1] = "#draggable1";
theimagestwo[2] = "#draggable2";
// Set inner HTML as theimagestwo[1] (which is "#draggable1")
document.getElementById("pagethree").innerHTML = theimagestwo[1];
因此,正如您所说,结果是“以文本形式出现”,因为它是文本。
如果我理解正确的话,你希望#pagethree 的内容是元素 #draggable1(而不是string "#draggable1"
).所以你应该将上面的代码替换为:
var theimagestwo = new Array();
theimagestwo[1] = $("#draggable1");
theimagestwo[2] = $("#draggable2");
$("#pagethree").empty().append(theimagestwo[1]);
关于javascript - 如何在数组中存储唯一的函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247700/