如何使用JQuery获取当前所选li的实例

时间:2013-09-18 06:59:04

标签: jquery html

我需要获取当前所选li的实例,这是动态生成的,我使用下面的代码:

使用$(this)不会给出当前所选li的实例。请参阅以下代码详情:

//在选择消息时添加类

$("ul").on("click", "li", function () { AttachEvents(); });

        function AttachEvents() {
            parentID = $(this).('li label[id="discussionID"]').val();
        };

下面是HTML代码:

   <ul id="ui-discussion">
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">24 Jul 2013</label><label>Loremipsumdolorsitamet,
                            consecteturadipisicingelit,seddoeiusmodtemporincididuntutlaboreetdoloremagnaaliqua.Ut</label><label
                                id="discussionID">983402</label></div>
            </div>
        </li>
        <ul>
            <li class="ui-discussion-childmessage">
                <div>
                    <div id="empImage">
                        <img src="Images/emp2.png"></div>
                    <div>
                        <label id="empName">
                            Vincent Cash</label><label id="chatdate">24 Jul 2013</label><label>Loremipsumdolorsitamet,consecteturadipisicingelit</label></div>
                </div>
            </li>
        </ul>
        <ul>
            <li class="ui-discussion-childmessage">
                <div>
                    <div id="empImage">
                        <img src="Images/emp3.png"></div>
                    <div>
                        <label id="empName">
                            Tucker Montgomery</label><label id="chatdate">24 Jul 2013</label><label>Loremipsumdolorsitamet,consecteturadipisicingelseddoeiusmod</label></div>
                </div>
            </li>
        </ul>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>Hi</label><label
                            id="discussionID">590079</label></div>
            </div>
        </li>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>This is a test message</label><label
                            id="discussionID">424769</label></div>
            </div>
        </li>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>New Message</label><label
                            id="discussionID">255351</label></div>
            </div>
        </li>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>Hi</label><label
                            id="discussionID">2030</label></div>
            </div>
        </li>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>hi hi hi</label><label
                            id="discussionID">154860</label></div>
            </div>
        </li>
        <ul>
            <li class="ui-discussion-childmessage">
                <div>
                    <div id="empImage">
                        <img src="Images/emp1.png"></div>
                    <div>
                        <label id="empName">
                            Arti Agarwal</label><label id="chatdate">9/18/2013</label><label></label></div>
                </div>
            </li>
        </ul>
        <ul>
            <li class="ui-discussion-childmessage">
                <div>
                    <div id="empImage">
                        <img src="Images/emp1.png"></div>
                    <div>
                        <label id="empName">
                            Arti Agarwal</label><label id="chatdate">9/18/2013</label><label></label></div>
                </div>
            </li>
        </ul>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>hi</label><label
                            id="discussionID">282729</label></div>
            </div>
        </li>
        <ul>
            <li class="ui-discussion-childmessage">
                <div>
                    <div id="empImage">
                        <img src="Images/emp1.png"></div>
                    <div>
                        <label id="empName">
                            Arti Agarwal</label><label id="chatdate">9/18/2013</label><label>hi</label></div>
                </div>
            </li>
        </ul>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>hi</label><label
                            id="discussionID">587583</label></div>
            </div>
        </li>
        <li class="ui-discussion-parentmessage ui-state-active">
            <div>
                <div id="empImage">
                    <img src="Images/emp1.png"></div>
                <div>
                    <label id="empName">
                        Gareth Sweet</label><label id="chatdate">9/18/2013</label><label>HI</label><label
                            id="discussionID">395700</label></div>
            </div>
        </li>
    </ul>

3 个答案:

答案 0 :(得分:0)

事件处理程序中的

this引用li

$(this).addClass('ui-state-active');

更新

$("ul").on("click", "li", AttachEvents );

    function AttachEvents() {
        parentID = $(this).find('label[id="discussionID"]').val();
    };

答案 1 :(得分:0)

  

$(this)未提供当前所选li的实例

尝试使用event参数代替this来访问当前元素,如下所示:

<ul class="target" >
            <li>Pear</li>
            <li>Apple</li>
            <li>Plum</li>
        </ul>

$(".target li").live( "click", function(event){
console.log(event.currentTarget);
});

控制台输出应该是这样的:

<li>Pear</li>

答案 2 :(得分:0)

这是一个工作示例:http://jsbin.com/eTUxAzU/1/ 而不是实时使用jquery。

parentID = $(this).find('li label[id="discussionID"]').val();
does not have val() method.

instead u should use 
parentID = $(e.target).parent().closest('li').find('label[id="discussionID"]').text();