0

我不擅长英语。请理解。

我想解决一个名为 testdom 的网站提供的问题。

https://www.testdome.com/questions/javascript/food-ranking/46764?visibility=3&skillId=2

你能帮我解决这个问题吗?谢谢你的提示。

祝你有美好的一天。

[我的代码]

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Ice Cream Flavors</title>
    <style>
    </style>
</head>
<body>

<ol>
    <li><button>Up!</button>Taco<button>Down!</button></li>
    <li><button>Up!</button>Pizza<button>Down!</button></li>
    <li><button>Up!</button>Eggs<button>Down!</button></li>
</ol>


</body>
<script>


    function setup() {
        // Write your code here
        let buttons = document.getElementsByTagName('button');


        for(let i=0; i< buttons.length; i++){
            buttons[i].onclick = function (){

                let btnName = this.innerText
                let li = this.parentElement.parentElement.children
                for(let i =0; i < li.length;i++){

                    (function(idx) {
                        li[idx].onclick = function() {

                            if(btnName == 'Up!'){
                                console.log('up')
                                let parent = this.parentNode
                                console.log(this.parentElement)
                                console.log(this.parentElement.children[idx])
                             //   this.parentNode.replaceChild(this.parentElement.children[idx],this.parentElement.children[idx-1])

                            }else{
                                console.log('down')
                            }
                        }
                    })(i);
                }

            }
        }
    }


    setup();
    document.getElementsByTagName('button')[2].click();
   // console.log(document.body.innerHTML);
</script>
</html>
4

0 回答 0