代码之家  ›  专栏  ›  技术社区  ›  Bullines

UpdatePanel中元素上的jQuery事件

  •  1
  • Bullines  · 技术社区  · 15 年前

    我在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时显示特殊消息范围,并显示以下内容:

    $(document).ready(function() {
        $("#clickableImage").click(function() {
            $("#specialMessage").show();
        });
    
        $("#specialMessage").draggable();
    });
    

    2 回复  |  直到 15 年前
        1
  •  1
  •   Ed James    15 年前
            <script type="text/javascript">
                var prm = Sys.WebForms.PageRequestManager.getInstance();
    
                prm.add_endRequest(function() {
                    bindPageEvents(); /// insert bind function here! [I have it as an actual function() to avoid writing it out twice when I use this method]
                });
            </script>
    

    将事件重新绑定到UpdatePanel中创建的元素。

    所以,你只是把

    function bindPageEvents(){
    $(document).ready(function() {
        $("#clickableImage").click(function() {
            $("#specialMessage").show();
        });

    $("#specialMessage").draggable();
    

    }); });

        2
  •  6
  •   Sampson    15 年前

    使用 $.live()

    $("#clickableImage").live("click", function() {
        $("#specialMessage").show();
    });
    

    #clickableImage 以及所有未来的实例。