0

我需要获取当前选定 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>
4

3 回答 3

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();
于 2013-09-18T07:41:39.980 回答
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>
于 2013-09-18T07:38:20.417 回答
0

this事件处理程序内部指的是li

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

更新:

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

    function AttachEvents() {
        parentID = $(this).find('label[id="discussionID"]').val();
    };
于 2013-09-18T07:00:56.717 回答