【问题标题】:Find next element of same type through Jquery通过Jquery查找下一个相同类型的元素
【发布时间】:2017-05-09 06:42:19
【问题描述】:

大家好,请查看下面的html。

    <div class="mainDiv2">
        <a href="home00.html" >home00</a>
        <a href="home0.html" >home0</a><br /><br />
        <h1>Employee List</h1>
        <a href="home.html" >home</a><br /><br />
        <a href="home2.html" >home2</a>
        <a href="home3.html" >home3</a>
        <h1>Employee List2</h1>
        <a href="home4.html">home4</a>
        <a href="home5.html">home5</a>
        <h1>Employee List3</h1>
        <a href="home6.html">home6</a>
        <a href="home7.html">home7</a>
        <a href="home8.html">home8</a>
        <a href="home9.html" class="active">home9</a>

        <p>
            {!PageTitle}
        </p>
    </div>
    <div>
        <a class="btn btn-default" id="prevRec" href="#">Pre</a>
        <a class="btn btn-default" id="nextRec" href="#">Next</a>
    </div>

我想要的是那个。如果在 href 上实现了一个类.active,那么我想获取相同类型的上一个和下一个元素 href 值。在当前的 html 中它将返回

上一篇:home8.html 下一个 : 空

在以下情况下

    <div class="mainDiv2">
        <a href="home00.html" >home00</a>
        <a href="home0.html" >home0</a><br /><br />
        <h1>Employee List</h1>
        <a href="home.html" >home</a><br /><br />
        <a href="home2.html" >home2</a>
        <a href="home3.html" >home3</a>
        <h1>Employee List2</h1>
        <a href="home4.html">home4</a>
        <a href="home5.html">home5</a>
        <h1>Employee List3</h1>
        <a href="home6.html" class="active">home6</a>
        <a href="home7.html">home7</a>
        <a href="home8.html">home8</a>
        <a href="home9.html" >home9</a>

        <p>
            {!PageTitle}
        </p>
    </div>

它会返回

前:home5.html 下一个:home7.html

注意:请记住,a 标签之间有 h1 标签。只是为了让你们知道,我已经有了这个问题的答案。但我想要不同的解决方案。我认为这不是正确的解决方案。

如果这是我的答案:

 $(document).ready(function () {
        var currActive = $('div[class="mainDiv2"]').find('a.active');

        if ($(currActive).nextAll('a').length > 0) {
            var nextHref = $(currActive).nextAll('a').attr("href")
            $("#nextRec").attr("href", nextHref);
        }
        else
            $("#nextRec").attr("href", 'http://www.google.com').text("Home");

        //--------------

        if ($(currActive).prevAll('a').length > 0) {
            var preHref = $(currActive).prevAll('a').attr("href")
            $("#prevRec").attr("href", preHref);
        }
        else
            $("#prevRec").attr("href", 'http://www.google.com').text("Home");
    });

谢谢..

【问题讨论】:

    标签: javascript jquery html performance find


    【解决方案1】:

    要实现这一点,您可以使用prevAll()nextAll() 来获取所需的元素,即使它们之间存在h1。试试这个:

    var $active = $('a.active');
    var prev = $active.prevAll('a').first().attr('href');
    var next = $active.nextAll('a').first().attr('href');
    
    console.log(prev);
    console.log(next);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="mainDiv2">
      <a href="home00.html">home00</a>
      <a href="home0.html">home0</a><br /><br />
      <h1>Employee List</h1>
      <a href="home.html">home</a><br /><br />
      <a href="home2.html">home2</a>
      <a href="home3.html">home3</a>
      <h1>Employee List2</h1>
      <a href="home4.html">home4</a>
      <a href="home5.html">home5</a>
      <h1>Employee List3</h1>
      <a href="home6.html" class="active">home6</a>
      <a href="home7.html">home7</a>
      <a href="home8.html">home8</a>
      <a href="home9.html">home9</a>
      <p>
        {!PageTitle}
      </p>
    </div>

    【讨论】:

      【解决方案2】:

      您可以通过消除任何不是链接的元素来尝试使用 prev/next 进行类似的操作,这将捕获多个活动类(如果存在):

      $('.mainDiv2').clone().find('*').remove('*:not("a")').end().find('.active').each(function(){
        var next = $(this).next('a').attr('href');
        var prev = $(this).prev('a').attr('href');
        console.log(prev== undefined?'google.com':prev,next == undefined?'google.com':next);
      })
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="mainDiv2">
        <a href="home00.html">home00</a>
        <a href="home0.html">home0</a><br /><br />
        <h1>Employee List</h1>
        <a href="home.html">home</a><br /><br />
        <a href="home2.html">home2</a>
        <a href="home3.html">home3</a>
        <h1>Employee List2</h1>
        <a href="home4.html">home4</a>
        <a href="home5.html">home5</a>
        <h1>Employee List3</h1>
        <a href="home6.html" class="active">home6</a>
        <a href="home7.html">home7</a>
        <a href="home8.html">home8</a>
        <a href="home9.html">home9</a>
        <a href="home10.html" class="active">home6</a>
      
        <p>
          {!PageTitle}
        </p>
      </div>

      【讨论】:

        猜你喜欢
        • 2018-12-16
        • 2011-07-09
        • 2011-04-09
        • 2010-12-20
        • 2016-01-19
        • 1970-01-01
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        相关资源
        最近更新 更多