简体   繁体   English

对动态元素使用JQuery选择器和“ this”

[英]Using JQuery selectors and “this” for dynamic elements

I have a page with a ton of documents for a client who would like users to be able to click on each article/project listed on the page, and for a small details pane to open beneath it reveling the content. 我有一个包含大量文档的页面,该页面供客户希望用户能够单击页面上列出的每个文章/项目,并在其下方打开一个小的详细信息窗格来显示内容。

I know this is fairly easy to do, particularly with JQuery, but I am not well-versed in the language and trying to take a stab at this kind of dynamic functionality. 我知道这很容易做到,尤其是使用JQuery,但是我对语言不精通,并试图尝试这种动态功能。 There are well over 50 documents, hence the reason for using "this" and trying to keep things dynamic rather than explicitly calling EACH of the 50 elements (and also bogging down load times). 有50多个文档,因此使用“ this”并试图使事物保持动态而不是显式调用50个元素的EACH的原因(并且还降低了加载时间)。

Here is my HTML (I've only included ONE of the elements in the list)... 这是我的HTML(我仅在列表中包括一个元素)...

    <ul class="paging" id="paging">
                <li><a href="">Economics of Ethanol and Bio-butanol as Gasoline Blendstocks<br />
                <strong>Categories:</strong> Oxygenated Fuels Issues, Bio-fuel Economics, Blendstock Valuation, Refining Economics, Refinery Modeling</a>
                <ul style="background:#f8f8f8; width:500px; height:200px;"><li>Here is the text that should appear beneath woot</li></ul>

                </li>

And here is my JQuery bit... 这是我的JQuery位...

    <script type="text/javascript">
        $(document).ready(function() {

            $('ul.paging > li > ul').hide();

            $('ul.paging > li').click(function() {

                $(this > ul).slideToggle();

            });
        });


    </script>

My elements is being hidden correctly, so I'm assuming my selectors are ok? 我的元素已正确隐藏,所以我假设我的选择器还可以吗? Anyhow, there's no working functionality with the roll-out. 无论如何,推出没有任何工作功能。 Any thoughts? 有什么想法吗?

Thank you, all help is very appreciated! 谢谢大家的帮助! Like I said, very new to this. 就像我说的那样,这很新。 :) Thanks again! :) 再次感谢!

The value of this is a DOM element. 的值this是一个DOM元素。 You need to use it as the base for jQuery's DOM traversal methods, like .children() . 您需要将其用作jQuery的DOM遍历方法的基础,例如.children()

$(this).children("ul").slideToggle();

$(this > ul) should look like $('ul', this) $(this > ul)应该看起来像$('ul', this)

Or 要么

$(this).find('ul').slideToggle();

The selector here is likely your problem: 这里的选择器可能是您的问题:

$(this > ul).slideToggle();

You might best use this: 您最好使用:

$(this).find('ul').slideToggle();

I would also suggest making CSS style for the ul 's hidden by default, that way they wouldn't potentially flash on the screen before the document ready event. 我还建议默认情况下将ul的CSS样式设置为隐藏,这样,在文档就绪事件发生之前,它们就不会在屏幕上闪烁。

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

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