簡體   English   中英

使用block / none顯示/隱藏jquery

[英]Show/hide jquery using block / none

任何人都可以告訴我如何使用此代碼在DIV之間顯示和隱藏數據(不隱藏和顯示 - 我想要另一種方式)?

我在.js文件中使用:

function toggle(sDivId) {
                var oDiv = document.getElementById(sDivId);
                oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none";
}

在.php文件中:

<div onclick="toggle('divContent1')" style="cursor: pointer;">Hide and show</div>
    <div id="divContent1">
    text here
    </div>
</div>

這段代碼工作得很好但是當頁面加載時我希望文本已經隱藏(不是在我點擊“隱藏和顯示”文本之后)。

謝謝!

您可以使用您擁有的代碼,但只需添加display:none; 在css。 或者在php里面的style="display:none;"

<div onclick="toggle('divContent1')" style="cursor: pointer;">Hide and show</div>
    <div id="divContent1" style="display:none;">
    text here
    </div>
</div>

jQuery還有一個你可以使用的toogle()函數。

演示

我可能誤解了你的問題,但如果你想隱藏在頁面加載的文本,你不能只是粘貼display:none標簽,如下所示:

<div id="divContent1" style="display: none">
text here
</div>
$('#clickMe').click(function(){
 $('#divContent1').toggle();

});

小提琴演示

您提供的代碼不使用jquery。 如果你想使用jquery這樣做,你可以改變這樣的事情。

function toggle(sDivId) {
    $("#"+sDivId).toggle();
}

並且你的php塊你要添加display:none; 最初隱藏內部div

<div onclick="toggle('divContent1')" style="cursor: pointer;">Hide and show</div>
    <div id="divContent1" style='display:none'>
    text here
    </div>
</div>

您可以使用toggle ()函數在show ()和hide ()之間交替切換。 否則,直接使用show ()和hide ()函數。

在body的onload事件中調用函數toggle('divContent1')

<body onload="toggle('divContent1')">

使用此代碼:

function load(){
   document.getElementByID('divContent1').style.display = 'none';
}

並在你的HTML中

<body onLoad = "load()">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM