Use onchange in a div

落花浮王杯 提交于 2019-11-27 07:39:08

问题


Is it possible to use onchange event in a <div>?

Because my <div> changes its text inside.

Can I do something like this?

<div id="name" onchange="calculateTotal();"></div> 

回答1:


Since you say you're using AJAX, why not execute the function when you update the text. I'm not sure if you use any library, but in case it's jQuery just do:

 $.ajax({ ...,
          success: function() {
               ... other things ...
               ... setting div text ...
               calculateTotal();
          }
       });



回答2:


No; the onchange attribute is only applicable to form field elements (input, select, textarea, etc).




回答3:


As you are changing the text yourself, just call calculateTotal when the AJAX call completes and the text has been placed in the element.

Example (using jQuery):

$('#name').load('GetFragment.php', calculateTotal);



回答4:


Onchange is called when user changed input value. So answer is yes, but it will have no effect. I assume you change content programmatically, so add simple code which will call this function




回答5:


You can use onblur event. Onblur event working on table,div etc.

<div id="name" onblur="calculateTotal();"></div>


来源:https://stackoverflow.com/questions/6676186/use-onchange-in-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!