我在asp.net中開發一個web應用程序。我正在使用jquery以編程方式在#messages-list中添加/附加消息,它顯示正常,但我想要顯示單擊msg時從右側流動的div。我添加了.click事件,但它不起作用。奇怪的是,當我粘貼靜態/手動的HTML代碼和評論jQuery的部分,附加HTML編程,點擊工作正常,我不知道問題在哪裏。而且在Chrome控制檯中沒有錯誤。請幫助。我的代碼如下。Jquery .click以編程方式添加html後無法正常工作
我的HTML代碼
<ul id="messages-list">
</ul>
<div id="MessagePane" style="height: 666px; display: none;">
<div class="inner-pane">
<div style="overflow: hidden;">
<a href="#" class="floatleft next-step message-close" data-pane="MessagePane">Close</a>
<a class="floatright" id="message-detail-date">July 9, 2013</a>
</div>
<div class="clear"></div>
<h4 id="message-detail-title">Important Message: Pinapple Shortage</h4>
<p id="message-detail-content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent.<br>
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla fermentum ornare dui sed commodo. Class aptent taciti sociosqu ad litora torquent.
</p>
</div>
</div>
我的JavaScript/jquery的部分
$("ul#messages-list").append('<li><a href="#" class="arrow-list message" data-pane="MessagePane"><div><div msgggggg</div></a></li>');
$('.arrow-list.message').click(function() {
alert('dsad');
$(this).parent().siblings().find('a').removeClass('transparent');
$(this).addClass('transparent');
var height = $('#body_inner').outerHeight();
if ($('#' + $(this).data('pane')).is(":visible")) {
$('#' + $(this).data('pane')).hide('slide', {
direction: 'right'
}, 300).css('height', height).show('slide', {
direction: 'right'
}, 500);
} else {
$('#' + $(this).data('pane')).css('height', height).show('slide', {
direction: 'right'
}, 500);
}
});
$('.message-close').click(function() {
$('.message').removeClass('transparent')
$('#' + $(this).data('pane')).hide('slide', {
direction: 'right'
}, 500);
});
可能重複[通過動態生成的元素引發的事件沒有被事件處理程序捕獲(http://stackoverflow.com/questions/12829963/events-triggered-by - 動態生成元素不被事件捕獲) –
和[事件綁定在動態創建的元素上?](http://stackoverflow.com/q/203198/218196) –