简体   繁体   中英

How can I Join two paragraphs to display inline together without jusitified text moving around?

I'm trying to take two paragraphs and join them together to display inline. I know I can use a span tag instead of p tags but my problem is this. I want the text to be justified in both paragraphs. I want to set the second paragraph to display:none (hidden) to start off. Using jQuery I will toggle the display:none to show the hidden text in the second paragraph. I don't want the text on the last line of the first paragraph to start moving around when the second paragraph is revealed. This is where I am stuck. I can join the paragraphs together but I'm getting movement on the last line of text because I am adding new text to that last line in the first paragraph. It's re-justifying the last line of text. It doesn't look good when this happens.

Remember, the text in the second paragraph will continue at the end of the first paragraph without a line break.

 $(function() { $('span[id=span2]').addClass('hidepar'); $('span[id=span1]').click(function() { $('span[id=span2]').fadeToggle(1000); }); }); 
 .hidepar { display: none; } #div1 { text-align: justify; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div1"> <span id="span1" style="display:inline">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.</span> <span id="span2">Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</span> </div> 

Thanks!

I don't know what your design looks like, but if your second paragraph just needs to seem to appear, you could go about it a few ways:

  1. Add a class to the second paragraph, display it inline, but set it to have the same color as your background. Then, use the jQuery event to adjust the color to be your text color. The problem is that Google will frown upon these shenanigans.

  2. You could also adjust the second paragraph's opacity level , instead of using display:none.

This assumes that there aren't other page elements that would be affected. Just two thoughts.

maybe you can change your code to this:

.hidepar {
visibility: hidden;
}
#div1 {
text-align: justify;
}

<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
$('span[id=span2]').addClass('hidepar');
$('span[id=span1]').click(function() {
$('span[id=span2]').css('visibility', 'visible');  
  });        
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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