UpdatePanel 中元素上的 jQuery 事件
我在 UpdatePanel 中有一些元素,根据不同的条件,这些元素可能会或可能会显示。
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
<ContentTemplate>
<asp:Panel ID="MyPanel" runat="server">
<img id="clickableImage" src="/path/to/image.png" alt="Clickable Image" />
<span id="specialMessage">You clicked on the image!</span>
<asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
我正在尝试将其连接起来,以便在单击 clickableImage IMG 时显示specialMessage SPAN,如下所示:
$(document).ready(function() {
$("#clickableImage").click(function() {
$("#specialMessage").show();
});
$("#specialMessage").draggable();
});
但是,由于 MyPanel 在页面加载时通常不可见(但稍后根据用户交互可能会可见),事件没有连接起来。有没有一种方法可以连接这些事件,即使 MyPanel 在初始页面加载时不可见?
I have some elements inside an UpdatePanel which may or may be displayed, depending on various conditions.
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
<ContentTemplate>
<asp:Panel ID="MyPanel" runat="server">
<img id="clickableImage" src="/path/to/image.png" alt="Clickable Image" />
<span id="specialMessage">You clicked on the image!</span>
<asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
I'm trying to wire it up so that the specialMessage SPAN is shown when the clickableImage IMG is clicked with the following:
$(document).ready(function() {
$("#clickableImage").click(function() {
$("#specialMessage").show();
});
$("#specialMessage").draggable();
});
However, since MyPanel is often not visible when the page loads (but it may be visible later based on user interaction), the events aren't hooked up. Is there a way that I can hook up these events even if MyPanel is not visible on the initial page load?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
$.live()
方法附加动态添加元素的逻辑。这将适用于所有当前的
#clickableImage
实例以及所有未来的实例。Use the
$.live()
method to attach the logic for dynamically-added elements.This will apply to all present instances of
#clickableImage
as well as all future instances too.将您的事件重新绑定到 UpdatePanel 中创建的元素。
所以,你只要把
<代码>
Will re-bind your events to elements created in an UpdatePanel.
So, you just put