gpt4 book ai didi

javascript - 生成列表时无法按升序打印数字数组

转载 作者:行者123 更新时间:2023-11-28 04:45:27 24 4
gpt4 key购买 nike

当选择“确定”符号时,我的代码会生成一个数字矩阵。但是,我想添加排序功能,以便在选择“按结果”按钮时按升序重新列出矩阵。关于我该怎么做有什么建议吗?

HTML 代码

<div class="rightDiv">
<div id = "pastcalcblock">
<h3> PAST CALCULATIONS </h3>
<input type = "text" size = "1" id = "text1"/>
<input type = "text" size = "1" id = "text2"/>
<input type = "text" size = "1" id = "text3"/>
<input type = "text" size = "1" id = "text4"/><br>
<input type = "button" value = "Ok" id = "operation" onClick = "display()"/>
<div id = "resultTab">
SORT<br>
<input type = "button" value = "As Entered" id = "enteredBut">
<input type = "button" value = "By Result" id = "resultBut" onClick() = "sortDisplay()"><br><br>
<div id="expressions">
</div>
</div>
</div>

Javascript代码

function display()
{
var arrayOne =[document.getElementById('text1').value,document.getElementById('text2').value,document.getElementById('text3').value,document.getElementById('text4').value ];

new_array=arrayOne.join(" ");
var para = document.createElement("p");
var t = document.createTextNode(new_array);
para.appendChild(t)
document.getElementById("expressions").appendChild(para);

}

function sortDisplay()
{
function doSort() {
var container = document.getElementById("expressions");
var elements = container.childNodes;
var sortMe = [];
for (var i=0; i<elements.length; i++) {


}

}

最佳答案

使用.textContent获取p元素中文本节点的内容。将其拆分为一个数组,对该数组进行排序,然后将其放回 DIV 中。

function sortDisplay()
{
function doSort() {
var container = document.getElementById("expressions");
var sortMe = container.childNodes[0].textContent.split(' ');
sort(sortMe);
container.innerHTML = '';
new_array=sortMe.join(" ");
var para = document.createElement("p");
var t = document.createTextNode(new_array);
para.appendChild(t)
document.getElementById("expressions").appendChild(para);
}

关于javascript - 生成列表时无法按升序打印数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43417844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com