且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测div上的内容更改事件

更新时间:2023-02-26 14:19:02

你喜欢吗? http://jsfiddle.net/Ralt/hyPQC/

document.getElementById( 't' ).onkeypress = function( e ) {
    var evt = e || window.event
    alert( String.fromCharCode( evt.which ) )
}​

不等待更改事件,这是没有意义的。

It's not waiting for a change event, it's kind of pointless.

相反,它正在听 onkeypress 事件。每次用户更改此div的内容(通过添加一个字符),它会触发该事件。

Instead, it's listening to the onkeypress event. Everytime a user changes the content of this div (by adding a character to it), it triggers the event.

您还可以看到如何获取点击的角色(使用 String.fromCharCode(evt.which))。

You can also see how to get the character clicked (using String.fromCharCode( evt.which )).

PS:一个完整​​的jQuery解决方案针对您的具体案例将是这个:

PS: a full jQuery solution for your specific case would be this:

$( '#product_descriptioncontent' ).on( 'keypress', function() {
    $( '#your-hidden-input' ).val( $( this ).text() )
    // Or
    $( '#your-hidden-div' ).text( $( this ).text() )
} )