由于 html.push,使用同一类的 jQuery 显示/隐藏 div 不起作用?

发布于 2024-12-20 21:30:52 字数 850 浏览 3 评论 0原文

目标是显示-隐藏位于各自标题下的文本,以便用户阅读标题并在用户想要阅读更多内容时显示或隐藏属于该标题的文本。

我尝试了到目前为止我能在这里找到的一切,我们正在讨论动态设置来自电子表格的文本,不能使用ID,必须使用.class,必须缺少一些东西,我有这个一段代码:

... html.push('

' + comment + '
');

但是当我尝试这个时显示隐藏代码没有任何反应,即使错误控制台没有显示任何内容。基本上我想显示-隐藏 .comments 类 div,并在每个 div 下方都有一个显示-隐藏切换链接。我说它们是因为 .comments div 是在从 Google 电子表格单元格/行提取文本时动态再现的(每个电子表格行一个 .comments div) )。我尝试了 .nextchildparent 但他们都与我离婚了,所以我不知道看起来是一个动态问题。到目前为止,我只能将所有 div 全局切换到可见或隐藏状态,但我需要独立切换各个 div。

我更喜欢 jQuery 解决方案,但到目前为止,任何有效的解决方案都是通过本机 JavaScript 实现的。

注意:如果跨浏览器截断函数会在每个 .comments div 中的多个单词(var)后附加一个 more-less 链接,那么我很乐意采用该选项。感谢您的帮助,请记住我仍在学习哈哈!

The object is to Show-Hide text located under their respective Titles, so a User reads the title and shows or hides text belonging to that title if the User wants to read more.

I tried whatever I could find so far on here, we're talking dynamically setting text coming from a spreadsheet, can't use IDs, must work with .class, must be missing something, I have this piece of code:

... html.push('<div class="comments">' + comment + '</div></div></div>');

but when I try this Show-Hide code nothing happens, even if the error console shows nothing. Basically I want to Show-Hide the .comments class divs with a show-hide toggle link located under each of them. I say them because the .comments divs are reproduced dynamically while extracting text coming from Google spreadsheet cells/row (one .comments div per spreadsheet row). I tried .next, child and parent but they all divorced me so I dunno looks like a dynamic issue. So far I only managed to globally toggle all divs to a visible or hidden state but I need to toggle independantly individual divs.

I prefer a jQuery solution but whatever worked so far was achieved with native javascript.

Note: If a cross-browser truncate function which would append a more-less link after a number of words (var) in each .comments divs would be easier to implement then I would gladly take that option. Thx for any help, remember I am still learning lol!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-12-27 21:30:52

我一直致力于一个完全 JS UI 项目,并让自己使用 $('', {properties }).appendTo(BaseElement) 最适合添加 HTML 元素,因为它每次都会适当地操作 DOM。

但是,如果您在其他地方推送时运气好,请在执行 $('.class').hide() 的行上设置断点,然后查看 $('.class').length 是什么。或者,如果无法在代码中设置断点,则可以将alert($('.class').length) 添加到代码中。如果它是 0,那么你的元素还没有正确添加到 DOM 中。更改为追加将确保它们是 DOM 的一部分,因此可通过 JQuery 定位。

I have been working on an entirely JS UI project and have brought myself to using $('', { properties }).appendTo(BaseElement) to work best for adding HTML elements because it appropriately manipulates the DOM every time.

If you are having good luck with push elsewhere, however, breakpointing on the line where you do your $('.class').hide() and see what $('.class').length is. Alternately, you can just add alert($('.class').length) to your code if you are unable to breakpoint the code. If it is 0, then your elements have not been properly added to the DOM. Changing to append will ensure they are part of the DOM and therefore targetable via JQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文