简体   繁体   English

单击按钮如何显示下一篇文章

[英]How to show next article on click of a button

I need to hide and show different articles on click of a button. 我需要单击按钮才能隐藏和显示其他文章。 This is like step by step instruction. 这就像逐步说明。 Here I have the code block below 这里有下面的代码块

<article class="content-1">Content 1
    <button type="button" class="continue">Continue</button>
</article>
<article class="content-2">Content 2
    <button type="button" class="continue">Continue</button>
</article>
<article class="content-3">Content 3
    <button type="button" class="continue">Continue</button>
</article>

JQUERY JQUERY

$('.continue').click(function()
    $(this).parents('div').next('article').show();
});

Now on click of the .continue button I need to hide the present article and show the next article. 现在单击.continue button我需要隐藏当前文章并显示下一篇文章。 Can anyone help me out to solve this thanks in advance. 任何人都可以帮助我提前解决此问题。

Note: Reaching the last article should stop the function. 注意:到达上article应停止该功能。

DEMO 演示

http://jsfiddle.net/kaqr6ysn/4/ http://jsfiddle.net/kaqr6ysn/4/

You could quite simply do this: 您可以很简单地做到这一点:

$('button.continue').click(function(){
  $(this).parent().hide().next().show();
});

You haven't explained what to do at the end, though. 不过,您最后没有解释要做什么。 Presumably you will omit the continue button from the last article ? 大概您会忽略上article的继续按钮吗?


Update: if you're wondering why your fiddle didn't work: 更新:如果您想知道为什么您的小提琴无法正常工作:

  • You didn't select jQuery on the fiddle 您没有在小提琴上选择jQuery
  • You omitted the { from line 1 您从第1行省略了{
  • You referenced div rather than article on line 2 您引用div而不是article 2行上的article
  • You neglected to .hide() the article 您忽略了.hide()文章

You can go with this solution: 您可以使用以下解决方案:

I have changed some css and html markup classes 我已经更改了一些CSS和HTML标记类

Changed class name, added common class name 更改班级名称,添加通用班级名称

<article class="content">Content 1
    <button type="button" class="continue">Continue</button>
</article>

use closest() to find nearest parent. 使用closest()寻找最近的父母。

$(".continue").on("click",function(){
    if($(this).closest(".content").next(".content").length > 0){
       $(this).closest(".content").hide();
       $(this).closest(".content").next(".content").show();
    }
});

CSS 的CSS

.content:not(:first-of-type) {
    display:none;
}

Demo 演示版

Try this : hide all article first then find next article to show it, if next article not present then stop the function. 尝试以下操作:先隐藏所有文章,然后查找下一篇文章以显示它,如果不存在下一篇文章,则停止该功能。

$(function(){
    $('.continue').click(function(){

      var next = $(this).closest('article').next();
      //check if next article is present or not
        if(next.length!=0)
        {
            $('.continue').closest('article').hide();
            next.show();
        }
     });
});

Demo 演示版

You can use closest() to get the parent article. 您可以使用closest()来获取父文章。 Then you can use .next() in with .hide() 然后您可以将.next().hide()

Script 脚本

$('.continue').on('click', function(){
    var article = $(this).closest('article');    
    if(article.next('article').length){
        article.next('article').show();
    }else{
        $('article').first().show(); //If you want to use continuous loop use it else just delete this line
    }    
    article.hide()
})

DEMO 演示

You have some errors in your js. 您的js中有一些错误。 You can try following: 您可以尝试以下操作:

$('.continue').click(function () {

    if ($(this).parent("article").next().length > 0) { //check if there is more articles
        $(this).parent("article").next().show(); //show next article
        $(this).parent("article").hide(); //hide present article
    }
});

fiddle 小提琴

try this:- 尝试这个:-

$('button').click(function(){
var ar = $(this).parent('article');
if (ar.next().length)
{
    ar.hide().next().show();
 }
});

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM