本文介绍了动态jQuery语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个jQuery语句:

I have this jQuery statement:

 $(currentEle).parent().parent("li").next().click()

其中currentEleli元素.

如果语句$(currentEle).parent().parent("li").next()的这一部分返回null,我想在该语句中添加.parent().parent("li").

If this part of the statement $(currentEle).parent().parent("li").next() returns null, I would like to add a .parent().parent("li") to the statement.

例如:$(currentEle).parent().parent("li").parent().parent("li").next().click(),依此类推.我添加了一个HTML列表,以使事情更清楚.从列表中,如果将currentEle设置为表示E的li,我想选择列表中的下一个元素,即"F". $(currentEle).parent().parent("li").next()将为null,因此$(currentEle).parent().parent("li").parent().parent("li")将完成此工作.那么如何使它更通用?

For example: $(currentEle).parent().parent("li").parent().parent("li").next().click() and so on. I have added an HTML list to make things more clear. From the list, if the currentEle is set to the li representing E, and I would like to select the next element in the list i.e. 'F'. $(currentEle).parent().parent("li").next()would be null and so $(currentEle).parent().parent("li").parent().parent("li") would do the job. So how do I make this more generic?

 <ul>
        <li>A</li>
        <li>B
            <ul>
                <li>C
                    <ul>
                        <li>D
                            <ul>
                                <li>E</li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>F</li>
            </ul>
        </li>
    </ul>

推荐答案

更新后的答案:

我在E li上添加了id以简化代码.

I added an id to the E li to simplify the code.

jQuery:

var value = ($('#theE').parents('li').filter(function(index) {
    return $(this).next('li').length;
}).next().text());

alert(value); //F

HTML:

 <ul>
        <li>A</li>
        <li>B
            <ul>
                <li>C
                    <ul>
                        <li>D
                            <ul>
                                <li id="theE">E</li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>F</li>
            </ul>
        </li>
    </ul>

JSFiddle演示版

JSFiddle DEMO

这篇关于动态jQuery语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:49