"document.getElementById(div).style.display = none"不起作用



当我点击一个按钮时,我试图隐藏三个div,但我的js不起作用。

    function display() {
    document.getElementById("Contentable").style.display = none;
    document.getElementById("savebtn").style.display = none;
    document.getElementById("stylebar").style.display = none;
    }

当我点击按钮时,会出现一个未捕获的引用错误,上面写着"none"未定义。

您需要将"none"作为字符串传递。

function display() {
    document.getElementById("Contentable").style.display = 'none';
    document.getElementById("savebtn").style.display = 'none';
    document.getElementById("stylebar").style.display = 'none';
}

否则,它将被解释为本例中未定义的变量名。

none应该在" "中,您也可以使用JQuery。

function display() {
    $("#Contentable").hide();
    $("#savebtn").hide();
    $("#stylebar").hide();
   }

你必须用双引号或单引号"none"来写none,就像这个

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
    width: 500px;
    height: 500px;
    background-color: lightblue;
}
</style>
</head>
<body>
<button onclick="myFunction()">Try it</button>
<div id="myDIV">
This is my DIV element.
</div>
<script>
function myFunction() {
    document.getElementById("myDIV").style.display = "none";
}
</script>
</body>
</html>

none必须在引号中,否则它将被视为变量

element.style.display = 'none';

如果你想让一个对象消失,但仍然占用HTML页面上的空间,你可以使用下面的代码:

document.getElementById("Contentable").style.visibility="hidden";
document.getElementById("savebtn").style.visibility="hidden";
document.getElementById("stylebar").style.visibility = "hidden";

否则,要回答您的问题,请复制粘贴以下代码:

document.getElementById("Contentable").style.display = "none";
document.getElementById("savebtn").style.display = "none";
document.getElementById("stylebar").style.display = "none";

Safari以不同的方式呈现内容,并使用UI覆盖进行下拉。您不能只是隐藏它们,但每次都可以刷新数组中匹配的选项。在这里,我使用jQuery每次清除一个选择选项并创建新选项:

myFunction();
function myFunction() {
  //alert("dins");
    var select = document.getElementById("selectArticles");
  var options = ["NouCreus", "Pic de l'Àliga", "Puigmal", "Finestrelles", "NouFonts", "Núria Estació"];
  
    var i, L = select.options.length - 1;
  for(i = L; i >= 0; i--) {
     select.remove(i);
  }
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("inputPatro");
    filter = input.value.toUpperCase();
  
  var el = document.createElement("option");
      el.textContent = "Vall de Núria";
      el.value = "-1";
      select.appendChild(el);
  for(var i = 0; i < options.length; i++) {
    var opt = options[i];
    if (opt.toUpperCase().indexOf(filter) > -1) {
      var el = document.createElement("option");
      el.textContent = opt;
      el.value = opt;
      select.appendChild(el);
    }
  }
}
<input type="text" id="inputPatro" onchange="myFunction()" placeholder="Filtre per nom.." title="Escriu un patró per filtrar les opcionw del desplegable">
<select id="selectArticles">
</select>

最新更新