我对任何东西的编码只有零度。我正在尝试建立一个小型的简单网站,并且差不多完成了,但是还有一个小问题需要解决。我知道在此之前已经有人问过这个问题,并且我试图弄清楚如何使这种情况对我有用,但不能解决。我什至尝试了简单的jquery淡入和淡出方法,但无法获取ID的正确信息或其他内容?
我要做的就是单击链接后在div之间淡入和淡出。
我在这里尝试并查看了许多示例,但仍然无法完全连接。

任何帮助,将不胜感激。

我在页面上有三个链接,可将三个不同的div加载到一个容器中。一切都在同一页面上,并且一切正常,除了单击链接时我无法让它们淡入和淡出。如果最有效,加载jquery库并以这种方式执行时我没有问题。

<head>

<script type="text/javascript">

function showDiv(idInfo) {
  var sel = document.getElementById('divLinks').getElementsByTagName('div');
  for (var i=0; i<sel.length; i++) {
    sel[i].style.display = 'none';
  }
  document.getElementById('container'+idInfo).style.display = 'block';
}
</script>


<style type="text/css">


#container1, #container2, #container3 {
    display:none;
    overflow:hidden;
    background-color: #E6E1E6

</style>



</head>

<body style="background-color: #E6E1E6">



<div id="container" style="position: fixed; width: 100%; z-index: 200;" >
    <div id="linkDiv" style="z-index: 100; position: absolute; width: 100%; text-align: center; font-family: Arial, Helvetica, sans-serif; margin-top: 20px;">
    <a href="#" onclick="showDiv('1');return false" style="margin-right: 10px">The Original Woman</a>
    <a href="#" onclick="showDiv('2');return false" style="margin-right: 10px; margin-left: 10px">CREDIT</a>
    <a href="#" onclick="showDiv('3');return false" style="margin-left: 10px">CONTACT</a>
  </div>
</div>

<!-- The 4 container content divs. -->
<div id="divLinks" style="width: 100%; height: 100%">
 <div id="container1" style="position: fixed; width: 100%; height: auto;" >
     <table cellpadding="0" cellspacing="0" style="width: 100%">
         <tr>
             <td style="width: 60%">&nbsp;</td>
             <td class="auto-style1" style="width: 40%">
             <img height="auto" src="asencio%20(7).jpg" width="100%" />&nbsp;</td>
         </tr>
     </table>
    </div>
    <div id="container2" style="position: fixed; width: 100%; height: auto;" >
        <table cellpadding="0" cellspacing="0" style="width: 100%">
            <tr>
                <td style="width: 50%">
                <img height="auto" src="mukai.jpg" width="100%" />&nbsp;</td>
                <td style="width: 50%">&nbsp;</td>
            </tr>
        </table>
    </div>
 <div id="container3" style="position: fixed; width: 100%; height: auto;" >
     <table cellpadding="0" cellspacing="0" style="width: 100%">
         <tr>
             <td style="width: 37%">
             <img height="auto" src="pandora_by_alifann.jpg" width="100%" />&nbsp;</td>
             <td style="width: 62%">&nbsp;</td>
         </tr>
     </table>
    </div>

<script type="text/javascript">
window.onload = function() { showDiv('1'); }
</script>

</div>


</body>

</html>

最佳答案

您提到使用jQuery,这是一个基本概念。代码注释应说明正在发生的事情。我通过添加一些类和一些数据属性对HTML进行了一些改动。



$("#linkDiv").on("click", "a", function(evt) { //use event bubbling so there is only one click hanlder
  evt.preventDefault(); //stop click event

  var anchor = $(this); //get the link that was clicked on

  if (anchor.hasClass("active")) { //If has the class, it is already is active, nothing to do
    return;
  }

  anchor.siblings().removeClass("active"); //find previous selectd link and unselect it
  anchor.addClass("active"); //add class to current link and select it

  var showTab = anchor.data("tab"); //read the data attribute data-tab to get item to show
  var visibleContainer = $(".tab-container:visible");
  var complete = function() { //function to call when fade out is complete
    $(showTab).stop().fadeIn(300);
  };
  if (visibleContainer.length) { //make sure w have something to hide
    $(visibleContainer).stop().fadeOut(100, complete); //if we do, fade out the element, when finished, call complete
  } else {
    complete(); //if first time, just show it
  }
}).find("a").eq(0).trigger("click"); //click on first link to load tab content.

.tab-container {
  display: none;
  overflow: hidden;
}
#container1 {
  background-color: #E60000;
}
#container2 {
  background-color: #00E100;
}
#container3 {
  background-color: #0000E6;
}
a.active {
  background-color: yellow;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container" style="position: fixed; width: 100%; z-index: 200;">
  <div id="linkDiv" style="z-index: 100; position: absolute; width: 100%; text-align: center; font-family: Arial, Helvetica, sans-serif; margin-top: 20px;">
    <a href="#" style="margin-right: 10px" data-tab="#container1">The Original Woman</a>
    <a href="#" style="margin-right: 10px; margin-left: 10px" data-tab="#container2">CREDIT</a>
    <a href="#" style="margin-left: 10px" data-tab="#container3">CONTACT</a>
  </div>
</div>

<!-- The 4 container content divs. -->
<div id="divLinks" style="width: 100%; height: 100%">
  <div id="container1" class="tab-container" style="position: fixed; width: 100%; height: auto;">
    <table cellpadding="0" cellspacing="0" style="width: 100%">
      <tr>
        <td style="width: 60%">&nbsp;</td>
        <td class="auto-style1" style="width: 40%">
          <img height="auto" src="asencio%20(7).jpg" width="100%" />&nbsp;</td>
      </tr>
    </table>
  </div>
  <div id="container2" class="tab-container" style="position: fixed; width: 100%; height: auto;">
    <table cellpadding="0" cellspacing="0" style="width: 100%">
      <tr>
        <td style="width: 50%">
          <img height="auto" src="mukai.jpg" width="100%" />&nbsp;</td>
        <td style="width: 50%">&nbsp;</td>
      </tr>
    </table>
  </div>
  <div id="container3" class="tab-container" style="position: fixed; width: 100%; height: auto;">
    <table cellpadding="0" cellspacing="0" style="width: 100%">
      <tr>
        <td style="width: 37%">
          <img height="auto" src="pandora_by_alifann.jpg" width="100%" />&nbsp;</td>
        <td style="width: 62%">&nbsp;</td>
      </tr>
    </table>
  </div>

10-06 04:40