Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - How to trigger an input tab selection when tab input is not selected?

I have a standard tabbed layout on a page. Screenshot of example below. enter image description here

Problem is each tab has a different height. So easiest solution I can find is to check when the input tab ID is selected, and then force a css height to the div behind it. This fixes the problem very nicely. This is the code I am using:

<script type='text/javascript'>
    window.onload = function () { 
      document.getElementById('tab1').onclick=function() {

      $("#internalcontainer").css("height","2600px");
      }    
      document.getElementById('tab2').onclick=function() {

      $("#internalcontainer").css("height","1000px");
      }  
      document.getElementById('tab3').onclick=function() {

      $("#internalcontainer").css("height","1200px");
      }  
      document.getElementById('tab4').onclick=function() {

      $("#internalcontainer").css("height","1200px");
      } 
      document.getElementById('tab5').onclick=function() {

      $("#internalcontainer").css("height","600px");
      }  
      document.getElementById('tab6').onclick=function() {

      $("#internalcontainer").css("height","2400px");
      }  
      document.getElementById('tab7').onclick=function() {

      $("#internalcontainer").css("height","800px");
      }  
}
</script>

Unfortunately this does not trigger the first input tab since when page loads the user is not selecting the tab - the first tab shows as default on page load.

How would I create a fallback where if no tab inputs are selected, revert to tab1 ID? Something like:

if tab1 selected = do this
if tab2 selected = do that
else = tab1 rule is selected
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not seeing your HTML, I would think you could so something like this

$function () { 
  $('.tab').on("click",function() { // any tab, use class="tab"
    $("#internalcontainer").height($(this).parent().find(".someDiv").height());
  }).eq(0).click(); // trigger the first onload or look at the URL
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...