首页 > 解决方案 > 你会如何在最后隐藏这个按钮?

问题描述

我想在代码到达最后时隐藏按钮,但它不会隐藏。我想知道你们会怎么做?

这是html-

<body>
<main class="container">
    <div class="countries">
    </div>
    <button class="btn-country">Where am I?</button>
    <div class="images"></div> 
</main> 
</body>

这是脚本 -

const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector('.countries');


 const renderError = function (message) {
        countriesContainer.insertAdjacentText('beforeend', message);
        countriesContainer.style.opacity = 1;
    };

    const getCountrydata3 = function (country) {
        fetch(`https://restcountries.eu/rest/v2/name/${country}`).then(res => res.json()).then(data => {
    
            renderCountry(data[0]);
    
            //set the neighbour country
            const neighbour = data[0].borders[0];
    
            //the guard clause
            if (!neighbour) return;
    
            //getting the data
    
            return fetch(`https://restcountries.eu/rest/v2/alpha/${neighbour}`)
        }).then(res => res.json()).then(data => renderCountry(data, 'neighbour')).catch(err => renderError(`Something went wrong! ${err.message}`)).finally(() => {
            console.log('damn button won\'t hide');
btn.style.visibility = hidden;  //<---**Here it is not working**
        })
    };
    
    btn.addEventListener('click', function () {
        getCountrydata3('sfsfsf');
    
    })

这个按钮不会隐藏在finally中。:S

标签: javascriptes6-promisefinally

解决方案


试试这个方法

btn.style.visibility = "hidden";

或者,

document.querySelector('.btn-country').style.visibility = "hidden";

或者,

document.querySelector('.btn-country').style.display ="none";

如果它不起作用,请尝试使用 jquery 或任何其他易于使用的库


推荐阅读