简体   繁体   English

jQuery:隐藏div的内容

[英]jquery: hiding contents of a div

I'm new to programming and JavaScript. 我是编程和JavaScript的新手。 I want to create a simple memory game with numbers. 我想用数字创建一个简单的记忆游戏。 The program is like this, when the user click on the button, numbers will be shown in 5 seconds and after that, numbers will hide until the user click on cards. 该程序是这样的,当用户单击按钮时,数字将在5秒钟内显示,然后数字将隐藏,直到用户单击卡。 Is the code below right? 下面的代码正确吗? I tried many codes but it didn't work 我尝试了很多代码,但是没有用

// {k1,...,k16} are the divs' ids and kart is div's classname
$(document).ready(function() {
  $("button").click(function() {
    $("#k1").html("2");
    $("#k2").html("3");
    $("#k3").html("1");
    $("#k4").html("3");
    $("#k5").html("5");
    $("#k6").html("6");
    $("#k7").html("1");
    $("#k8").html("6");
    $("#k9").html("4");
    $("#k10").html("7");
    $("#k11").html("8");
    $("#k12").html("8");
    $("#k13").html("5");
    $("#k14").html("7");
    $("#k15").html("2");
    $("#k16").html("4");
  });

});

function myFunction() {
  setTimeout(function() {
    $('.kart').empty();
  }, 4000);
}

Did you want to do this? 您要这么做吗?

 //k1,k2 and... are div ids and kart is divs classname $(document).ready(function() { $("button").click(function() { $("#k1").data("number", "2"); $("#k2").data("number", "3"); $("#k3").data("number", "1"); $("#k4").data("number", "3"); $("#k5").data("number", "5"); $("#k6").data("number", "6"); $("#k7").data("number", "1"); $("#k8").data("number", "6"); $("#k9").data("number", "4"); $("#k10").data("number", "7"); $("#k11").data("number", "8"); $("#k12").data("number", "8"); $("#k13").data("number", "5"); $("#k14").data("number", "7"); $("#k15").data("number", "2"); $("#k16").data("number", "4"); myFunction(); }); }); function myFunction() { $(".kart").each(function(i, a) { $(a).html($(a).data("number")); }); setTimeout(function() { // if you want to use class name for selector, you must set the class first! $(".kart").html(""); }, 1000); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="k1" class="kart"></div> <div id="k2" class="kart"></div> <div id="k3" class="kart"></div> <div id="k4" class="kart"></div> <div id="k5" class="kart"></div> <div id="k6" class="kart"></div> <div id="k7" class="kart"></div> <div id="k8" class="kart"></div> <div id="k9" class="kart"></div> <div id="k10" class="kart"></div> <div id="k11" class="kart"></div> <div id="k12" class="kart"></div> <div id="k13" class="kart"></div> <div id="k14" class="kart"></div> <div id="k15" class="kart"></div> <div id="k16" class="kart"></div> <button type="button">click me</button> 

I hope this can help you out: basically you want to hide the content of the div so you can do the hide(); 我希望这可以对您有所帮助:基本上,您想隐藏div的内容,以便可以执行hide(); function then you said you will show the card for seconds then do a 功能,然后您说您将显示卡片几秒钟,然后执行

setTimeout(function() {

    $(".kart").html();

}, 1000);

and use the .show() function. 并使用.show()函数。

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

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