首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让jQuery索引列表项以正确的方式排序

如何让jQuery索引列表项以正确的方式排序
EN

Stack Overflow用户
提问于 2017-06-30 22:23:36
回答 1查看 57关注 0票数 0

我有下面的代码,当点击打开右边的相关文章时,标题链接列表。默认情况下会显示第一篇文章,而其他文章将隐藏起来,直到单击下一个标题。我有的是默认的文章,显示喜欢我想要的,但标题链接时,点击显示的文章在错误的顺序。

代码语言:javascript
运行
复制
$(function codeAddress() {
  $('.posts-box').html($('.hidden-posts ul li div article:eq(' + $('.test-titles ul li div').index($(this).parent()) + ')').html());
  window.onload = codeAddress;

  $('a').click(function() {
    $('.posts-box').html($('.hidden-posts ul li div article:eq(' + $('.test-titles ul li div').index($(this).parent()) + ')').html());
  });
});
代码语言:javascript
运行
复制
.hidden {
  display: none;
}

.site-main {
  display: grid;
  grid-column-gap: 10px;
  grid-template-columns: 1fr 1fr;
}

.half {
  align-items: center;
  display: flex;
  justify-content: center;
  padding: 20px;
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="site-main">
  <div class="half test-titles">
    <ul>
      <li>
        <div>
          <a href="#" title="Test post Two">Test post Three</a>
        </div>
      </li>
      <li>
        <div>
          <a href="#" title="Test post Two">Test post Two</a>
        </div>
      </li>
      <li>
        <div>
          <a href="#" title="Test post one">Test post one</a>
        </div>
      </li>
      <li>
        <div>
          <a href="#" title="Hello world!">Hello world!</a>
        </div>
      </li>
    </ul>
  </div>
  <div class="hidden hidden-posts">
    <ul>
      <li>
        <div>
          <article>
            <header class="entry-header">
              <h2>Hello world!</h2>
            </header>
            <div class="entry-content">
              <p>Ugh four dollar toast cray authentic single-origin coffee brooklyn put a bird on it, intelligentsia hashtag vaporware lumbersexual yuccie occupy. Church-key man bun biodiesel, shaman disrupt single-origin coffee meggings lyft leggings. Listicle
                street art tumblr twee heirloom, scenester whatever master cleanse viral la croix umami pickled typewriter affogato. Vaporware celiac fanny pack</p>
            </div>
          </article>
        </div>
      </li>
      <li>
        <div>
          <article>
            <header class="entry-header">
              <h2>Test post one</h2>
            </header>
            <div class="entry-content">
              <p>Direct trade YOLO chia art party authentic, tumeric pok pok vinyl iPhone +1 palo santo. Jean shorts pop-up banjo freegan, thundercats chambray mumblecore heirloom. Seitan 8-bit yr.</p>
            </div>
          </article>
        </div>
      </li>
      <li>
        <div>
          <article>
            <header class="entry-header">
              <h2>Test post Two</h2>
            </header>
            <div class="entry-content">
              <p>Put a bird on it tousled you probably haven&#8217;t heard of them intelligentsia affogato chia health goth. Taiyaki kickstarter pinterest, twee distillery listicle chartreuse gentrify iPhone literally photo booth leggings kale chips.</p>
            </div>
            <!-- .entry-content -->
          </article>
        </div>
      </li>
      <li>
        <div>
          <article>
            <header class="entry-header">
              <h2>Test post Three</h2>
            </header>
            <div class="entry-content">
              <p>Tbh sriracha ramps taiyaki YOLO seitan hoodie farm-to-table cornhole waistcoat beard dreamcatcher godard affogato. Air plant stumptown you probably haven&#8217;t heard of them</p>
            </div>
            <!-- .entry-content -->
          </article>
        </div>
      </li>
    </ul>
  </div>
  <div class="half">
    <article class="posts-box">
    </article>
  </div>

我知道我可能可以做一些事情来使JS更整洁一点,但我希望首先让它正常工作。

jsfiddle上查看。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-07-01 00:53:53

我建议使用这个函数:

代码语言:javascript
运行
复制
$(function codeAddress() {
    var postBox = $('.posts-box');
    var articleHeaders = $('.entry-header');
    function getPostContent(post){
        var content = '';
        articleHeaders.each(function(){
            var h1 = $(this).find('h2'); 
            if(post.html()==h1.html()){
                content = $(this).parent().html();
            }
        });
        postBox.html( content);
    }
    $('a').click(function() { 
        getPostContent($(this));       // get content by link title
    }); 
    getPostContent($('a').eq(0));      // if you want initial content
});

https://codepen.io/FaridNaderi/pen/WOMBaL

虽然这不是最好的方法,但希望它能帮助你解决代码问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44848754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档