简体   繁体   English

仅当2个或更多段落时在1段落之后插入按钮

[英]Insert Button After 1 Paragraph Only If 2 or More Paragraphs

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

jQuery(document).ready(function($) {

if ( $('p').length < 1 ) { 

$('p:last-child').after('<div id="toggle" class="btn">Read More</div>');

$('p').not('.entry-content p:first-child').wrap('<div class="text" />');  

}

$("#toggle").click(function() {

    var elem = $("#toggle").text();

    if (elem == "Read More") {

      $("#toggle").text("Read Less");
      $("#text").slideDown("slow");

    } else {

      $("#toggle").text("Read More");
      $("#text").slideUp("fast");
    }

  }); });



<p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>

<p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>

<p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>

I only want to add the button after the first paragraph if there's 2 or more paragraphs. 如果有两个或更多段落,我只想在第一段之后添加按钮。

The jQuery adds the button after the 1st paragraph. jQuery在第一段之后添加按钮。 I don't want it added if there's only 1 paragraph of text in each article. 如果每篇文章只有一个文本段落,我不希望添加它。 I only want it added when there's at least 2 paragraphs. 我只希望在至少有2个段落时添加它。

Update : Basically, if there's only 1 paragraph, i don't need any of the jQuery to run so i guess i simply need a conditional. 更新:基本上,如果只有1段,我不需要运行任何jQuery,所以我想我只需要一个条件。

Update 2 : Like this but for paragraphs http://jsfiddle.net/RyqJ9/ 更新2:像这样,但对于段落http://jsfiddle.net/RyqJ9/

You have to check for condition for each of the text div with $(".text").each(function() with if((textBox.find('p').length)>1) so that if there are two or more p then you will show the button for that text div 您必须使用$(".text").each(function()if((textBox.find('p').length)>1)检查每个文本div的条件,以便如果有两个或更大的p ,则将显示该文本div的按钮

Here is a sample code. 这是示例代码。

 $(document).ready(function(){ $(".content .entry-content").each(function(){ var contentBox = $(this); if (contentBox.find('p').length > 1){ var lastOne = contentBox.find('p').length - 1; contentBox.append('<div id="toggle" class="btn">Read More</div>'); $('.content .entry-content p').not('.entry-content p:first-child').wrap('<div class="text" />'); contentBox.find(".text").hide(); contentBox.find(".btn").click(function(){ if($(this).attr('class').indexOf('btnDown')==-1){ contentBox.find(".text").slideDown("slow"); $(this).addClass("btnDown"); $(this).html("Read Less"); }else{ contentBox.find(".text").slideUp("slow"); $(this).removeClass("btnDown"); $(this).html("Read More"); } }); } }); }); 
 .text{ background-color:green; margin:5px 0; } .btn{ background-color:red; } .content{ border:2px solid black; margin:5px 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content"> <div class="entry-content"> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> </div> </div> <div class="content"> <div class="entry-content"> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> </div> </div> <div class="content"> <div class="entry-content"> <p>This is an example of a post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p> </div> </div> 

Have you tried something like this (untested)? 您是否尝试过类似的操作(未经测试)? Or if you're trying to append it to the bottom of the paragraph, rather than after it, use append() instead of after() . 或者,如果您尝试将其追加到段落的底部而不是后面,请使用append()而不是after()

$(function($) {
  if($('p:nth-child(2)').length) {
    $('p:first-child').after('<div id="toggle" class="btn">Read More</div>');
  }
});

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

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