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