Javascript 使用block/none显示/隐藏jquery

Javascript 使用block/none显示/隐藏jquery,javascript,jquery,Javascript,Jquery,谁能告诉我如何使用这段代码在DIV's之间显示和隐藏数据(不是隐藏和显示-我想要另一种方式) 我在.js文件中使用: function toggle(sDivId) { var oDiv = document.getElementById(sDivId); oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none"; } 在.php文件中: <

谁能告诉我如何使用这段代码在DIV's之间显示和隐藏数据(不是隐藏和显示-我想要另一种方式)

我在.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。或者在php中的
style=“display:none;”

隐藏和显示
此处文本
jQuery还有一个可以使用的函数


您可以使用您拥有的代码,但只需添加
display:none。或者在php中的
style=“display:none;”

隐藏和显示
此处文本
jQuery还有一个可以使用的函数


您可以使用
切换
()功能在
显示
()和
隐藏
()之间交替切换。否则,直接使用
show
()和
hide
()功能。

您可以使用
toggle
()功能在
show
()和
hide
()之间交替切换。否则,直接使用
show
()和
hide
()功能。

我可能误解了你的问题,但是如果你想在页面加载时隐藏文本,你就不能粘贴一个display:none标签,如下所示:

<div id="divContent1" style="display: none">
text here
</div>

此处文本

我可能误解了你的问题,但是如果你想在页面加载时隐藏文本,你就不能粘贴一个display:none标签,如下所示:

<div id="divContent1" style="display: none">
text here
</div>

此处文本
}))

}))


在主体的onload事件中调用函数
toggle('divContent1')

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

在主体的onload事件中调用函数
toggle('divContent1')

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

您提供的代码没有使用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>
隐藏和显示
此处文本

您提供的代码没有使用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>
隐藏和显示
此处文本
使用以下代码:

function load(){
   document.getElementByID('divContent1').style.display = 'none';
}
在你的html中

<body onLoad = "load()">

使用以下代码:

function load(){
   document.getElementByID('divContent1').style.display = 'none';
}
在你的html中

<body onLoad = "load()">


这不是jquery,你可以用CSS隐藏元素,或者在页面加载上调用你的函数。这不是jquery,你可以用CSS隐藏元素,或者在页面加载上调用你的函数。谢谢接受。请向上投票并接受回答。谢谢接受。请向上投票并接受回答