简体   繁体   English

HTML & JavaScript 如何使第 n 行文本出现第 n 次点击?

[英]HTML & JavaScript How to Make nth Lines of Text Appear with nth Number of Clicks?

I am very new to html and JavaScript, but I am an advanced novice at understanding programming.我对 html 和 JavaScript 非常陌生,但我是理解编程的高级新手。 I am looking for a way to make nth lines of text appear on nth button clicks of a button and when there are no more text lines to show the button does nothing.我正在寻找一种方法,使第 n 行文本出现在第 n 次按钮单击时,并且当没有更多文本行显示该按钮时什么也不做。 I imagine that there should be some index to keep track of the amount of clicks and a way to pass a number of clicks that should occur to some function, but I am having trouble finding anything.我想应该有一些索引来跟踪点击次数,并有一种方法可以将一些应该发生的点击次数传递给某些功能,但是我找不到任何东西。 Can someone point me in the right direction or demonstrate the basics of what I must do.有人可以指出我正确的方向或演示我必须做的基础知识。 Here is a visual demo of what I want to happen on the webpage:这是我想要在网页上发生的事情的视觉演示:

Click This点击这里

Click This点击这里
this line appears on first click这行出现在第一次点击

Click This点击这里
this line appears on first click这行出现在第一次点击
this line appears on second click此行出现在第二次点击

Click This点击这里
this line appears on first click这行出现在第一次点击
this line appears on second click此行出现在第二次点击
... ...
this line appears on nth click此行出现在第 n 次点击

Here is some code that does a bit what I am expecting.这里有一些代码可以满足我的期望。 Is there any changes I can make to make it work more generally?我是否可以进行任何更改以使其更普遍地工作?

 $(function() { $("#visible").click(function() { if (typeof this.counter == 'undefined') { this.counter = 0; } $('#invisible' + this.counter).toggleClass("show"); ++this.counter; }); });
 .hide { display: none; } .show { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="visible">Click This</p> <p id="invisible0" class="hide">this line appears on the first click</p> <p id="invisible1" class="hide">this line appears on the second click</p> <p id="invisible2" class="hide">this line appears on the third click</p>

Thanks in advance,提前致谢,
Vectorjon向量

This might not be optimal, but at least it's something you can start with.这可能不是最佳的,但至少它是你可以开始的。 Basically I set the font-size (in this case 12px) and then added that height + 2px (14px) to the size of the div container.基本上我设置了font-size (在本例中为 12px),然后将该height + 2px (14px)添加到 div 容器的大小。

https://jsfiddle.net/hassench/r7cgjzus/9/ https://jsfiddle.net/hassench/r7cgjzus/9/

 var expand = () => { $('.expandable').css('height', $('.expandable').height() + 14); }
 .expandable { font-size: 12px; height: 15px; overflow: hidden; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class='expandable' onclick='expand()'> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam libero ex, vehicula id egestas sed, iaculis vel enim. Curabitur interdum sollicitudin turpis, sit amet semper magna rutrum pharetra. Donec blandit, enim at volutpat feugiat, nunc orci placerat nunc, eu placerat tortor libero nec est. Aliquam erat volutpat. Ut dapibus tellus metus, a lacinia risus gravida quis. Nullam placerat maximus risus ut tristique. Mauris rhoncus sagittis erat, eget mollis ligula. Nulla accumsan fermentum dictum. </div>

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

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