按值升序对图像数组进行排序

cardShop 是一个带有子项的 div -> (具有不同 ID 和值的图像)每个卡值看起来像这个 2000,500 或这个 1500,200 我想按数组中的第一个数字按值升序对图像进行排序。该函数由右上角的蓝色按钮调用。


这是我到目前为止所做的。


HTML文档


<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Document</title>

    <script src="script.js"></script>

    <style>

        #toSort{

        background-color: #3b5998;

        width:50px;

        height:50px;

        float:right;

    }</style>

</head>

<body>

    <div id = "cardShop" style="overflow:scroll">

        <div id ="toSort" onclick="sorting()"></div>

        <img id="card"  height="400" width="250" src="./cardImages/blueEyes.jpg" value = 3000,2500>

        <img id="card2" height="400" width="250" src="./cardImages/blue.jpg"  value = 1400,1200>

        <img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value = 1200,700>

        <img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value = 3200,2500>

        <img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value = 2500,2100>

        <img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value = 2500,2100>

        <img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value = 2000,1700>

        <img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value = 2300,2100>

        <img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value = 2400,2000>

        <img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value = 1800,1600>

        <img id="card11" height="400" width="250" src="./cardImages/curse.jpg"  value = 2800,2200>

        <img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value = 4500,3800>

    </div>

</body>

</html>

JavaScript 文件


function sorting() {

    let deck = [].slice.call(document.getElementById("cardShop").children)

    for (let i = 2; i < deck.length; i++) {

        let elementValue = deck[i].getAttribute('value').split(",").map(Number);

    }

}


回首忆惘然
浏览 153回答 4
4回答

白衣染霜花

const sorting = () => {&nbsp; &nbsp; const images = document.getElementsByTagName('img');&nbsp; &nbsp; const deck = [].slice.call(images);&nbsp; &nbsp; const container = document.getElementById('cardShop');&nbsp; &nbsp; const deckSorted = deck.sort((a, b) => {&nbsp; &nbsp; &nbsp; &nbsp;const aToSort = a.getAttribute('value').split(',')[0];&nbsp; &nbsp; &nbsp; &nbsp;const bToSort = b.getAttribute('value').split(',')[0];&nbsp; &nbsp; &nbsp; &nbsp;return aToSort - bToSort;&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for(let i = 0; i < deck.length; i++) {&nbsp; &nbsp; &nbsp; deck[i].parentNode.removeChild(deck[i]);&nbsp; &nbsp; &nbsp; container.append(deckSorted[i]);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}<div id="cardShop" style="overflow:scroll">&nbsp; &nbsp; &nbsp; &nbsp; <div id ="toSort" onclick="sorting()">Sort</div>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card"&nbsp; height="400" width="250" src="./cardImages/blueEyes.jpg" value = 3000,2500>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card2" height="400" width="250" src="./cardImages/blue.jpg"&nbsp; value = 1400,1200>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value = 1200,700>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value = 3200,2500>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value = 2500,2100>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value = 2500,2100>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value = 2000,1700>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value = 2300,2100>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value = 2400,2000>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value = 1800,1600>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card11" height="400" width="250" src="./cardImages/curse.jpg"&nbsp; value = 2800,2200>&nbsp; &nbsp; &nbsp; &nbsp; <img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value = 4500,3800></div>您可以使用排序来完成此操作,然后删除旧元素并附加新元素,如下所示。

qq_遁去的一_1

const setup = () => {&nbsp; const toSort = document.querySelector('#toSort');&nbsp; toSort.addEventListener('click', sorting);};const sorting = () => {&nbsp; let cardShop = document.querySelector('#cardShop');&nbsp;&nbsp;&nbsp; let dek = [...cardShop.querySelectorAll('img')]&nbsp; &nbsp; .sort((a,b) => {&nbsp; &nbsp; &nbsp; let valuesA = a.getAttribute('value').split(',');&nbsp; &nbsp; &nbsp; let valuesB = b.getAttribute('value').split(',');&nbsp; &nbsp; &nbsp; let comparer = valuesA[0].localeCompare(valuesB[0]);&nbsp; &nbsp; &nbsp; if(comparer === 0)&nbsp; &nbsp; &nbsp; &nbsp; comparer = valuesA[1].localeCompare(valuesB[1]);&nbsp; &nbsp; &nbsp; return comparer;&nbsp; &nbsp; })&nbsp; &nbsp; .map(node=>cardShop.appendChild(node));};//loadwindow.addEventListener('load', setup);#toSort{&nbsp; background-color: #3b5998;&nbsp; width:50px;&nbsp; height:50px;&nbsp; float:right;}<!DOCTYPE html><html>&nbsp; <body>&nbsp; &nbsp; <div id = "cardShop" style="overflow:scroll">&nbsp; &nbsp; &nbsp; <div id ="toSort"></div>&nbsp; &nbsp; &nbsp; <img id="card"&nbsp; height="400" width="250" src="./cardImages/blueEyes.jpg" value="3000,2500">&nbsp; &nbsp; &nbsp; <img id="card2" height="400" width="250" src="./cardImages/blue.jpg"&nbsp; value="1400,1200">&nbsp; &nbsp; &nbsp; <img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value="1200,700">&nbsp; &nbsp; &nbsp; <img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value="3200,2500">&nbsp; &nbsp; &nbsp; <img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value="2500,2100">&nbsp; &nbsp; &nbsp; <img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value="2500,2100">&nbsp; &nbsp; &nbsp; <img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value="2000,1700">&nbsp; &nbsp; &nbsp; <img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value="2300,2100">&nbsp; &nbsp; &nbsp; <img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value="2400,2000">&nbsp; &nbsp; &nbsp; <img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value="1800,1600">&nbsp; &nbsp; &nbsp; <img id="card11" height="400" width="250" src="./cardImages/curse.jpg"&nbsp; value="2800,2200">&nbsp; &nbsp; &nbsp; <img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value="4500,3800">&nbsp; &nbsp; </div>&nbsp; </body></html>

LEATH

你走在正确的道路上,我为你完成了代码。该代码将所有图像推送到一个包含 json 元素的数组,该数组包含元素和值(左侧)。然后,它按正确的顺序对它们进行排序,删除所有图像并按正确的顺序再次插入它们。工作示例: https:&nbsp;//playcode.io/537993function sorting() {let deck = [].slice.call(document.getElementById("cardShop").children)let images = []let button = document.getElementById('toSort');for (let i = 1; i < deck.length; i++) {&nbsp; &nbsp; let elementValue = deck[i].getAttribute('value').split(",").map(Number);&nbsp; &nbsp; // add all values to a array with json info about the element&nbsp; &nbsp; images.push({&nbsp; &nbsp; &nbsp; 'element': deck[i],&nbsp; &nbsp; &nbsp; 'value': elementValue[0]&nbsp; &nbsp; });}// sort them on the valueimages.sort((a, b) => {&nbsp; return a.value - b.value;})// remove all elements in the divdocument.getElementById('cardShop').innerHTML = '';// add the button againdocument.getElementById('cardShop').appendChild(button);&nbsp; // Add the images again&nbsp; images.forEach(image => {&nbsp; &nbsp; document.getElementById('cardShop').appendChild(image.element)&nbsp; })}

开心每一天1111

您的 HTML 标记已损坏。确保将其重构为<img ... value="1200,2500" />而不是<img ... value=1 200,2500 />,因为 Javascript 无法处理它。function sorting () {&nbsp; &nbsp;const imgWrapper = document.getElementById('cardShop')&nbsp; &nbsp;const deck = [].slice.call(imgWrapper.getElementsByTagName('img'))&nbsp; &nbsp;deck.sort((currentImage, nextImage) => {&nbsp; &nbsp; &nbsp; const currentImageValue = parseInt(currentImage.getAttribute('value').split(',')[0])&nbsp; &nbsp; &nbsp; const nextImageValue = parseInt(nextImage.getAttribute('value').split(',')[0])&nbsp; &nbsp; &nbsp; return currentImageValue - nextImageValue&nbsp; &nbsp;})&nbsp; &nbsp;// at this point the array has been sorted, but not the actual HTML!&nbsp; &nbsp;console.log(deck)&nbsp; &nbsp;// optional - if you want to redraw them with the new order in the HTML&nbsp; &nbsp;const oldImgHTMLCollection = [].slice.call(imgWrapper.getElementsByTagName('img'))&nbsp; &nbsp;oldImgHTMLCollection.forEach(oldImg => imgWrapper.removeChild(oldImg))&nbsp; &nbsp;deck.forEach(newImg => imgWrapper.appendChild(newImg))}sorting()<!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <title>Document</title>&nbsp; <script src="script.js"></script>&nbsp; <style>&nbsp; </style></head><body>&nbsp; <div id="cardShop" style="overflow:scroll">&nbsp; &nbsp; <div id="toClose"></div>&nbsp; &nbsp; <div id="toSort"></div>&nbsp; &nbsp; <img id="card" height="400" width="250" src="./cardImages/blueEyes.jpg" value="3000,2500">&nbsp; &nbsp; <img id="card2" height="400" width="250" src="./cardImages/blue.jpg" value="1400,1200">&nbsp; &nbsp; <img id="card3" height="400" width="250" src="./cardImages/orange.jpg" value="1200,700">&nbsp; &nbsp; <img id="card4" height="400" width="250" src="./cardImages/blackDrag.jpg" value="3200,2500">&nbsp; &nbsp; <img id="card5" height="400" width="250" src="./cardImages/spider.jpg" value="2500,2100">&nbsp; &nbsp; <img id="card6" height="400" width="250" src="./cardImages/dark.jpg" value="2500,2100">&nbsp; &nbsp; <img id="card7" height="400" width="250" src="./cardImages/grill.jpg" value="2000,1700">&nbsp; &nbsp; <img id="card8" height="400" width="250" src="./cardImages/gaia.jpg" value="2300,2100">&nbsp; &nbsp; <img id="card9" height="400" width="250" src="./cardImages/redEyes.jpg" value="2400,2000">&nbsp; &nbsp; <img id="card10" height="400" width="250" src="./cardImages/flamee.jpg" value="1800,1600">&nbsp; &nbsp; <img id="card11" height="400" width="250" src="./cardImages/curse.jpg" value="2800,2200">&nbsp; &nbsp; <img id="card12" height="400" width="250" src="./cardImages/tripple.jpg" value="4500,3800">&nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5