Use onchange in a div

2019-01-20 09:12发布

问题:

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