如何重复循环div(How to repeatedly loop through divs)
我有一个脚本,可以在单击“下一个”或“上一个”按钮时更改div显示属性。 我可以单击Next并查看下一个div或单击Previous以查看上一个; 但是,一旦它到达结尾,classList变为undefined或null。 我想要做的是,当第一次加载页面时,我希望用户单击Next直到结束,然后单击循环中的第一个div。 此外,如果用户点击第一页上的Previous,那么我希望用户看到最后一个div。 任何帮助表示赞赏。 谢谢! 以下是代码..
var yogNumber = 0; var yogs = document.querySelectorAll('.yog'); function next() { yogNumber++; if (yogs.length > yogNumber) { document.querySelector('.active').classList.remove('active'); yogs[yogNumber].classList.add('active'); } } function previous() { yogNumber--; if (yogs.length > yogNumber) { document.querySelector('.active').classList.remove('active'); yogs[yogNumber].classList.add('active'); } }
.yog { display: none } .active { display: block }
<div class="yog active"> <div id="heading">Text 1</div> </div> <div class="yog"> <div id="heading">Text 2</div> </div> <div class="yog"> <div id="heading">Text 3</div> </div> <div class="yog"> <div id="heading">Text 4</div> </div> <br /> <div id="left" onclick="previous()"> <</div> <br /> <div id="right" onclick="next()">></div>
我目前得到的错误是“TypeError:无法读取未定义的属性'classList'”。
I have a script which changes the div display property on click on Next or Previous buttons. I am able to click on Next and see the next div or click on Previous to see the previous one; however, once it reaches to the end the classList becomes undefined or null. What I want to do is, when the page is loaded for the firs time, I want the user to click on Next until the end and then to the first div like in a loop. Also, if the user clicks on Previous from the first page, then I would want the user to see the last div. Any help is appreciated. Thanks! Below is the code..
var yogNumber = 0; var yogs = document.querySelectorAll('.yog'); function next() { yogNumber++; if (yogs.length > yogNumber) { document.querySelector('.active').classList.remove('active'); yogs[yogNumber].classList.add('active'); } } function previous() { yogNumber--; if (yogs.length > yogNumber) { document.querySelector('.active').classList.remove('active'); yogs[yogNumber].classList.add('active'); } }
.yog { display: none } .active { display: block }
<div class="yog active"> <div id="heading">Text 1</div> </div> <div class="yog"> <div id="heading">Text 2</div> </div> <div class="yog"> <div id="heading">Text 3</div> </div> <div class="yog"> <div id="heading">Text 4</div> </div> <br /> <div id="left" onclick="previous()"> <</div> <br /> <div id="right" onclick="next()">></div>
The error which I am getting at the moment is "TypeError: Cannot read property 'classList' of undefined".
原文:https://stackoverflow.com/questions/38430742
相关问答
更多-
您的QT是否与STL兼容的启用选项一起编译? 也许你可以使用fromUtf8或QString的其他静态函数之一。 Is your QT compiled with STL compatible enabled option? Maybe you can use fromUtf8 or one of other static functions of QString.
-
有一个名为fromUtf8的QString函数使用一个const char* : QString str = QString::fromUtf8(content.c_str()); There's a QString function called fromUtf8 that takes a const char*: QString str = QString::fromUtf8(content.c_str());
-
不要使用赋值运算符初始化std::string对象,而是直接初始化变量。 const std::string file = qfile.toStdString(); 除此之外,请确保您知道QString::toStdString()使用的编码方式; 与QString不同, std::string是编码不可知的(它是一串字节,而不是字符)。 Don't use the assignment operator to initialize the std::string object but rather in ...
-
因此,赋值运算符必须作为成员函数来实现(参见例如这里 ) QString a, b; std::string c, d; a = c; d = b; 将无法工作。 相反,你可能想要考虑一个自由函数,沿着这条线 assignStrings(a,c); // or perhaps... a = toQString(c); The assignment operator must be implemented as member function (see eg here), thus QString a ...
-
创建一个QPainter并在其构造函数中传递QImage对象。 然后调用QPainter的drawText函数将文本绘制到图像中。 所以,假设你已经创建了一个名为img的QImage对象... QPainter painter(img); painter.drawText(xPos, yPos, text); 您还可以在QPainter上设置所需的字体和画笔,以定义文本的外观。 Create a QPainter and pass it the QImage object in its construct ...
-
如何将QString转换为std :: string? 在将QString转换为std :: string时应记住的事情之一是QString是UTF-16编码,而std :: string可能有任何编码。 QString qs; // Either this if you use UTF-8 anywhere std::string utf8_text = qs.toUtf8().constData(); // or this if you on Windows :-) std::string curr ...
-
将不同类型的QList转换为QString的静态函数(Static function to convert QList of different types to QString)[2024-01-01]
笔记: 假设Product::at(int)正在返回一个Transaction ,给定了前面的问题。 我也假设OP在他写“静态”时表示“内置” 可以使用内置函数删除for 。 尽管如此,一些(很多?)会发现新语法不太容易理解。 QString Product::toString() { QStringList aggregate; std::transform(m_transactions.begin(), m_transactions.end(), ... -
你可以调用readAll()两次。 第二次,流位于文件结尾,因此readAll()没有任何可读的内容并返回空字符串。 在你的调试输出中打印fileText 。 You call readAll() twice. The second time, the stream is positioned at end-of-file, and so readAll() has nothing to read and returns an empty string. Print fileText in your deb ...
-
将整数转换为std :: pair接受的std :: string(converting an integer to a std::string accepted by an std::pair)[2022-07-22]
失败的原因: if(verbose) string ratio = intToStr(hp) + "/" + intToStr(maxHP()); // the block of the if is now over, so any variables defined in it // are no longer in scope 是变量的范围仅限于它定义的块。 The reason that this fails: if(verbose) string ratio = intToS ... -
如何在Qt中将std :: string转换为QString?(How to convert std::string to QString in Qt? [duplicate])[2023-10-06]
函数QString::fromStdString返回字符串的副本。 但是在你的代码中,这个函数的结果只是被忽略了。 尝试这个: const QString str = QString::fromStdString(latitude); 在它之后你将有QString str ,其内容与std::string latitude内容相同。 Function QString::fromStdString returns a copy of the string. But in your code result o ...