Javascript 在 div 中使用 onchange

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6676186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:45:57  来源:igfitidea点击:

Use onchange in a div

javascripthtmlonchange

提问by Rafael Morais dos Santos

Is it possible to use onchangeevent in a <div>?

是否可以onchange在 a 中使用事件<div>

Because my <div>changes its text inside.

因为我<div>在里面改变了它的文字。

Can I do something like this?

我可以做这样的事情吗?

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

采纳答案by pimvdb

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,那为什么不在更新文本时执行该函数呢?我不确定您是否使用任何库,但如果它是 jQuery,请执行以下操作:

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

回答by duskwuff -inactive-

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

不; 的onchange属性是仅适用于表单域元素(inputselecttextarea等等)。

回答by Guffa

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

当您自己更改文本时,只需calculateTotal在 AJAX 调用完成且文本已放置在元素中时调用即可。

Example (using jQuery):

示例(使用 jQuery):

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

回答by SergeS

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

当用户更改输入值时调用 Onchange。所以答案是肯定的,但不会有任何影响。我假设您以编程方式更改内容,因此添加将调用此函数的简单代码

回答by Sumit Kumar Gupta

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

您可以使用 onblur 事件。Onblur 事件在 table、div 等上工作。

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