Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用timeout隐藏div?_Javascript - Fatal编程技术网

Javascript 如何使用timeout隐藏div?

Javascript 如何使用timeout隐藏div?,javascript,Javascript,这里我调用JavaScript函数,它将文本和图像附加到div,现在我想在2秒后隐藏div。我该怎么做 代码: 像这样的东西可能就是你要找的 function showdiv(city, imagesrc, timeout) { // Show div document.getElementById('city-order').innerHTML = ""; document.getElementById('order-product').innerHTML = "";

这里我调用JavaScript函数,它将文本和图像附加到div,现在我想在2秒后隐藏div。我该怎么做

代码:


像这样的东西可能就是你要找的

function showdiv(city, imagesrc, timeout) {

    // Show div
    document.getElementById('city-order').innerHTML = "";
    document.getElementById('order-product').innerHTML = "";
    document.getElementById('product-list-display').style.display = "block";
    var order_placed_city = document.getElementById('city-order');
    var content = document.createTextNode(city);
    order_placed_city.appendChild(content);    
    var product_order = document.getElementById('order-product');
    var elem = document.createElement("img");
    product_order.appendChild(elem);
    elem.src = imagesrc;

  // Hide div after n milliseconds
  setTimeout(function() {
   document.getElementById('product-list-display').style.display = "none";      
  }, timeout);
}

像这样的东西可能就是你要找的

function showdiv(city, imagesrc, timeout) {

    // Show div
    document.getElementById('city-order').innerHTML = "";
    document.getElementById('order-product').innerHTML = "";
    document.getElementById('product-list-display').style.display = "block";
    var order_placed_city = document.getElementById('city-order');
    var content = document.createTextNode(city);
    order_placed_city.appendChild(content);    
    var product_order = document.getElementById('order-product');
    var elem = document.createElement("img");
    product_order.appendChild(elem);
    elem.src = imagesrc;

  // Hide div after n milliseconds
  setTimeout(function() {
   document.getElementById('product-list-display').style.display = "none";      
  }, timeout);
}

当前是执行以下操作的see代码:

showDiv()
上:

  • 隐藏
    #产品列表显示
  • 超时后
    -ms追加一些内容
  • 您可以简单地在隐藏div的计时函数末尾添加另一个
    窗口

    function showdiv(city, imagesrc, timeout) {
    
      window.setTimeout(function() {
        /* ... */
    
        // You could add it here
        window.setTimeout(hideDiv)
      },timeout);
    
      /* ... */
    
    }
    
    然后添加另一个函数:

    function hideDiv() {
        // Your hiding code here
    }
    
    我建议您考虑一个框架,比如jQuery。这将增加可读性,降低复杂性。

    当前正在查看执行以下操作的代码:

    showDiv()
    上:

  • 隐藏
    #产品列表显示
  • 超时后
    -ms追加一些内容
  • 您可以简单地在隐藏div的计时函数末尾添加另一个
    窗口

    function showdiv(city, imagesrc, timeout) {
    
      window.setTimeout(function() {
        /* ... */
    
        // You could add it here
        window.setTimeout(hideDiv)
      },timeout);
    
      /* ... */
    
    }
    
    然后添加另一个函数:

    function hideDiv() {
        // Your hiding code here
    }
    
    我建议您考虑一个框架,比如jQuery。这将增加可读性,降低复杂性。

    如果只想隐藏元素,请使用“显示:隐藏”,如果希望元素完全消失,请使用“不显示”:

    function hideDiv(elementID,timeout) {
        window.setTimeout(function() {
            document.getElementById(elementID).style.display = "none";
        },timeout);
    }
    

    如果只想隐藏图元,请使用“显示:隐藏”;如果希望图元完全消失,请使用“不显示”:

    function hideDiv(elementID,timeout) {
        window.setTimeout(function() {
            document.getElementById(elementID).style.display = "none";
        },timeout);
    }
    
    showdiv('城市','http://placehold.it/200x200(2000年)
    -也许?
    showdiv('city','http://placehold.it/200x200’,2000)
    -也许吧?