简体   繁体   English

Javascript显示/隐藏,在页面加载时显示第一个div

[英]Javascript show/hide, show first div on page load

Can you help me to make the first div show on page load? 您能帮我在页面加载时显示第一个div吗?

 function showStuff(element) { var tabContents = document.getElementsByClassName('tabContent'); for (var i = 0; i < tabContents.length; i++) { tabContents[i].style.display = 'none'; } var tabContentIdToShow = element.id.replace(/(\\d)/g, '-$1'); document.getElementById(tabContentIdToShow).style.display = 'block'; } 
 .tabContent { display:none; } 
 <a href="#contenuto"><div tabindex="1" class="tabs"><div id="tabs1" onclick="showStuff(this)">CARATTERISTICHE</div><div class="triangle-down-tab"></div></div></a> <a href="#contenuto" ><div tabindex="2" class="tabs"><div id="tabs2" onclick="showStuff(this)">DESTINATARI</div><div class="triangle-down-tab"></div></div></a> <a href="#contenuto"><div tabindex="3" class="tabs"><div id="tabs3" onclick="showStuff(this)"><i class="fa fa-calendar" style="color:#000000;"></i> CALENDARIO</div><div class="triangle-down-tab"></div></div></a> <a name="contenuto"><hr></a> <div id="tabs-1" class="tabContent"> <p>tab 1</p> </div> <div id="tabs-2" class="tabContent"> <p>tab 2 tab 2 </p> </div> <div id="tabs-3" class="tabContent"> <p>tab 3 tab 3 tab 3</p> </div> 

This is my actual code. 这是我的实际代码。 jsFiddle Thanks! jsFiddle谢谢!

You could try running a function when the document is ready. 您可以在文档准备好后尝试运行功能。

$(document).ready(function () {  
    showTab("tabs-1");

function showTab(divId) {
    //Get the element
    var divElement= document.getElementbyId(divId);
    //Set the css property  "display" from "none" to be "block";
    divElement..style.display = "block";
}
}):

The function should run once the page has fully loaded. 页面完全加载后,该函数应运行。 Let me know how it goes. 让我知道事情的后续。

I sure can. 我当然可以 When you do this kind of stuff best use css. 当您执行此类操作时,最好使用CSS。 That way when the dom loads the css will kick in and your desired effect will show. 这样,当dom加载时,css将启动,并且您想要的效果将显示出来。

Further more its easier to understand and easier to code up. 此外,它更易于理解和编写代码。

.tabContent  {
    display:none;
}
.tabContent.active  {
    display:block;
}

Then in the HTML 然后在HTML中

<div id="tabs-1" class="tabContent active">

So when the page loads tab one is active 因此,当页面加载选项卡时,第一个处于活动状态

Then in your JS 然后在你的JS里

function showStuff(element)  {
    var tabContents = document.getElementsByClassName('tabContent');
    for (var i = 0; i < tabContents.length; i++) { 

      tabContents[i].className="tabContent";
    }

    var tabContentIdToShow = element.id.replace(/(\d)/g, '-$1');
    document.getElementById(tabContentIdToShow).className="tabContent active";
}

Updated fiddle! 更新小提琴!

https://jsfiddle.net/rb5c5095/3/ https://jsfiddle.net/rb5c5095/3/

We could improve things since we know all the tabs will be made invisible at boot up and tab 1 will show. 我们可以改善情况,因为我们知道所有选项卡在启动时都将变为不可见,并且选项卡1将显示。 So when a tab is clicked we could just search the tab who has .active class and remove it, then apply the .active class to the new tab. 因此,当单击选项卡时,我们可以仅搜索具有.active类的选项卡并将其删除,然后将.active类应用于新选项卡。 This would have the benefit that any extra css you add in your html markup would not be removed by the JS code, but i reckon you can work that out and if you can't get back to me i can show you :-) 这样做的好处是,您在html标记中添加的任何额外的CSS都不会被JS代码删除,但是我认为您可以解决这一问题,如果您无法回复我,我可以告诉您:-)

Here I am invoking the function (upon page load) that tweaks the css of the desired block; 在这里,我要调用的函数(在页面加载时)会调整所需块的CSS; Same can be achieved by $(document).ready; 可以通过$ {document).ready;来实现。 I took this approach to avoid jquery; 我采用这种方法来避免使用jquery。

window.onload =  showDivOne();

function showDivOne() {
    document.getElementById("tabs-1").style.display = "block";
}

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

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