简体   繁体   English

在javascript中隐藏或显示按钮

[英]hide or show button in javascript

I am trying to hide or show a button in javascript, onload the button should be hidden. 我正在尝试在javascript中隐藏或显示button在加载按钮时应该隐藏。

function hideButton(){
    var x = document.getElementById('myDIV');
    x.style.display = 'none';
}

If data.nextPageURL has a string the button should be visable, if it does not have a string it should be hidden. 如果data.nextPageURL有一个字符串,则该按钮应该可见,如果没有字符串,则应将其隐藏。

var x = document.getElementById('myDIV');
if(data.nextPageURL){
   x.style.display = 'block';
}
else if(data.nextPageURL == "") {
    x.style.display = 'none';
}

but don't know where I am going wrong. 但不知道我要去哪里错。

Full ajax code: 完整的ajax代码:

function loadMore(url, data1) {
    $.ajax({
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        'type': 'POST',
        'url': url,
        'data': JSON.stringify(data1),
        'dataType': 'json',
        success: function (data) {
            //var json = JSON.stringify(data);
            var table = document.getElementById("searchList");
            for (var i=0; i < data.businesses.length ; i++) {
                var business = data.businesses[i];
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
            }
            var x = document.getElementById('myDIV');
                if(data.nextPageURL == ""){
                    x.style.display = 'none';
            }
            else {
                x.style.display = 'block';
            }

you have a logical issue in your code 您的代码中存在逻辑问题

var x = document.getElementById('myDIV');
if(data.nextPageURL){
   x.style.display = 'block';
} else {
    x.style.display = 'none';
}

If data.nextPageURL is here you show your button else you hide it. 如果data.nextPageURL在此处,则显示按钮,否则将其隐藏。

Open the snippet and write anything inside the nextPageURL , then the div will appear. 打开代码段并在nextPageURL内编写任何内容,然后div将出现。

 var x = document.getElementById('myDIV'); function hideButton() { if (data.nextPageUR) { x.style.display = 'block'; } else { x.style.display = 'none'; } } hideButton(); 
 #myDIV { height: 50px; width: 50px; background: blue; } 
 <div id='myDIV'></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM