【问题标题】:Creating Tabs with jQuery but display content at the top使用 jQuery 创建选项卡,但在顶部显示内容
【发布时间】:2026-01-25 10:40:01
【问题描述】:

我尝试了一些选项卡脚本,但我想我正在寻找更具体的东西。

基本上,我想要显示 div 并隐藏所有其他内容的东西,就像选项卡一样。

和这个很相似

http://jsfiddle.net/6UcDR/2/

但我希望能够使用按钮以外的东西。我将设置图像像画廊一样水平排列。当单击一个图像时,另一个 div 标记中的某些内容会显示在其正上方的中间。单击另一张图像时,显示的图像将隐藏,而显示另一张图像。几乎就像在那个 jsfiddle 演示中所做的一样。

但是,我希望能够使用某种类型的 div ID/类来将图像与内容联系起来......所以

<div id="blahblah"> Related Content Here </div><div id="blahblah"> Image Here </div>

或者类似的东西

<div id="content123" class="tab_content" style="display: block;"> content here <li class="active"><a href="#content123" rel="episodelist">Episode List</a></li>

这让我可以很容易地进入一个 php 循环,在那里我可以通过使用图像 ID 等来提取多个图像及其内容。

再次,我想要显示在我将拥有的链接或图像上方的内容。

所以它看起来像这样......

显示的动态内容 图片1 图片2 图片3 图片4 图片5

此动态内容会根据所点击的图片进行切换。

如果有人可以帮忙,那就太好了!

【问题讨论】:

  • 你可以用任何元素替换按钮,只需要具有相同的ID。
  • 当 ID 最初不同时,我该怎么做。这就是为什么我很困惑。
  • 用任何元素替换按钮,但分配他们现在拥有的id,每个ofc不同的id
  • ID 不应该与 div 中的 ID 匹配吗?我不太明白为什么ID不同。我已经尝试过这段代码,但它对我不起作用:(
  • 按钮是控件。显示/隐藏 div。 js 中的第一个函数表示“单击 id=w,隐藏 ID b、c、d,显示 id a。对于每个按钮,都有一个函数告诉它单击时要做什么,在这种情况下,要显示或隐藏哪些 div。

标签: jquery html css


【解决方案1】:

在学习 jQuery 时,由于 ID 选择器非常具体,因此很容易专注于对所有事物都需要一个 ID。当面对每组 ID 的多个场景时,使用 ID 的编码可能会变得复杂。但是有很多方法可以使用其他方法来匹配不同的元素,这些方法可以大大简化代码,而无需使用任何 ID。

例如,在您的按钮和演示中的内容项周围添加一个包装器,可以使用index() 方法编写代码:

HTML:

<div id="content">
  <div id=a style="display:none">1</diV>
  <div id=b style="display:none">2</diV>
  <div id=c style="display:none">3</diV>
  <div id=d style="display:none">4</diV>
</div>
<div id="button_wrap">
  <input type=button value=1 id=w/>
  <input type=button value=2 id=x/>
  <input type=button value=3 id=y/>
  <input type=button value=4 id=z/>
</div>

JS:

$(function(){
    $('#button_wrap input').click(function(){
      /* hide all content, show item that  matches index of this button*/
      $('#content').children().hide().eq( $(this).index() ).show()
    });
});

为相似的项目添加类也是选择器的一大帮手

演示:http://jsfiddle.net/6UcDR/38/

API 参考:

http://api.jquery.com/index/

http://api.jquery.com/eq/

花一些时间查看 API 中的各种遍历方法和示例。了解各种遍历的作用将帮助您生成更动态的布局和用户界面

【讨论】:

  • 我也会这样做,+1
  • 我在这里尝试过这段代码,点击按钮不显示任何内容。我把它放在循环中,&lt;?php the_ID(); ?&gt; 只返回一个数字。 &lt;script type="text/javascript"&gt; $('#button_wrap input').click(function(){ $('#content').children().hide().eq( $(this).index() ).show() }) &lt;/script&gt; 这是它的 HTML 部分 pastebin.com/epPj24ka
  • 确保将所有 jquery 包装在 document.ready 中。 jsfiddle 自动执行此操作api.jquery.com/ready 我用ready wrapper 更新了答案
  • 阅读此内容以更好地理解原因:docs.jquery.com/How_jQuery_Works
  • 我尝试了以下&lt;script type="text/javascript"&gt; $(document).ready(function () { $('#button_wrap input').click(function(){ $('#content').children().hide().eq( $(this).index() ).show() }) }; &lt;/script&gt;
【解决方案2】:
<script src="../../jquery-1.8.0.js"></script>
    <script src="../../ui/jquery.ui.core.js"></script>
    <script src="../../ui/jquery.ui.widget.js"></script>
    <script src="../../ui/jquery.ui.tabs.js"></script>
    <link rel="stylesheet" href="../demos.css">
    <script>
    $(function() {
        $( "#tabs" ).tabs();
    });
    </script>
<div class="demo">

<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Nunc tincidunt</a></li>
        <li><a href="#tabs-2">Proin dolor</a></li>
        <li><a href="#tabs-3">Aenean lacinia</a></li>
    </ul>
    <div id="tabs-1">
        <p>Image1</p>
    </div>
    <div id="tabs-2">
        <p>Image2</p>
    </div>
    <div id="tabs-3">
        <p>Image3</p>
    </div>
        <div id="tabs-4">
        <p>Image4</p>
    </div>
 <div id="tabs-5">
        <p>Image5</p>
    </div>

</div>

</div>

【讨论】:

  • 我需要点击图片来显示内容。不是相反。
【解决方案3】:
$(".contet").each( function() {
  $(this).hide();
});
$(".tab").on("click", function() {
  var s = $(this).attr("id");
  var g = $("#"+s+".contet").html();
  $(".content").html(g);
});

Working Fiddle With Pure jQuery

【讨论】:

  • 这不是我想要的。同样,我希望内容位于顶部。正如您从我的原始帖子中看到的那样,显示的动态内容和图像示例。我正在寻找一个非常简单的脚本,类似于我为 jsfiddle 提供的脚本。我不想添加其他插件或来源等。
  • 现在看看我的答案!并且接受它!
  • 这看起来更好,但我无法让它在我的页面上工作。那里的元素是不可点击的。另外,我将如何让所有内容在启动时不可见,而只有第一个可见。
  • “第一个可见的”我已经在小提琴中完成了。从那里复制代码。而且,你得到了什么错误?您不必更改类名和 ID!否则,如果您愿意,请在您的问题中添加 HTML 代码。