简体   繁体   English

jQuery类更改仅动作一次

[英]jQuery class changed only acts once

I am changing button style when through jQuery when a button is clicked. 我在单击按钮时通过jQuery更改按钮样式。 Below is the code. 下面是代码。

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnDiv .down").click(function () {
            $(this).addClass("click");
        });
    });

Since i have enclosed the #btnDiv in UpdatePanel the above functionality acts only once. 由于我已经将#btnDiv括在UpdatePanel中,因此上述功能仅起作用一次。 What is the reason and a work around for the problem. 是什么原因以及解决该问题的方法。

When you render your updatePanel, you need to execute those javascript once again, otherwise the former bound event will not be there, after updating. 呈现updatePanel时,您需要再次执行那些javascript,否则更新后,先前的绑定事件将不存在。

Take a look at jQuery.on() 看看jQuery.on()

If you have a jQuery Version before 1.7, you maybe need to use the .delegate() or .live() function. 如果您的jQuery版本.delegate() 1.7,则可能需要使用.delegate().live()函数。

So your code could be: 因此,您的代码可能是:

$(document).ready(function () {
        $("#btnDiv .down").on("click",function () {
            $(this).addClass("click");
        });
    });

You don't need to render the above code every time you update your panel. 您不需要在每次更新面板时都呈现以上代码。 Just execute once. 只需执行一次。

Assuming the "UpdatePanel" dynamically change its content. 假设“ UpdatePanel”动态更改其内容。 Then you need to use jquery .on() (or .delegate() ) function to bind events for dynamically updated elements. 然后,您需要使用jquery .on() (或.delegate() )函数为动态更新的元素绑定事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM