How jquery listens to changes in div content

Source: Internet
Author: User

We do e-commerce. The javascript framework uses jQuery. during the development process, we encountered the problem listed in the above title: how to listen to the changes in div content.
First give the final code (subsequent analysis ):
Copy codeThe Code is as follows:
Var title = $ ("B. facility ");
Var title = $ ('# title'); // the element I want to monitor
Title. bind ('domainserted', function (e ){
Alert ('element now contains: '+ response (e.target).html ());
});

The solution is as follows:
Let's first review the definition and usage of the change () method in the jQuery event:
When the value of an element changes, a change event occurs.
This event applies only to text fields and textarea and select elements.
The change () function triggers the change event, or specifies the function that runs when a change event occurs.
Note: When used for select elements, the change event occurs when an option is selected. When used in text field or text area, this event occurs when the element loses focus.
But there is a problem where the change method of div content is not mentioned. What should we do?
Subsequent Baidu keywords: jquery div Content Changes: fruitless;
Continuing, bing keywords: jquery how to listen div change find a relevant document http://stackoverflow.com/questions/2712124/jquery-listen-to-changes-within-a-div-and-act-accordingly
A rough understanding is to use a Custom Event to handle the problem. The Code is as follows:
Copy codeThe Code is as follows:
$ ('# Laneconfigdisplay'). bind ('contentchanged', function (){
// Do something after the div content has changed
Alert ('woo ');
});
// This will call the above function
$ ('# Laneconfigdisplay'). trigger ('contentchanged ');

However, the content of contentchanged is not described and Tracing continues.
Keywords: jquery how to listen div change find a relevant document
Continue, bing keywords: jquery contentchanged find a relevant document http://stackoverflow.com/questions/1449666/create-a-jquery-special-event-for-content-changed
This article details the definition of contentchanged content. The Adoption Code is as follows:
Copy codeThe Code is as follows:
JQuery. fn. watch = function (id, fn ){
Returnthis. each (function (){
Var self = this;
Var oldVal = self [id];
$ (Self). data (
'Watch _ timer ',
SetInterval (function (){
If (self [id]! = OldVal ){
Fn. call (self, id, oldVal, self [id]);
OldVal = self [id];
}
}, 100)
);
});
Returnself;
};
JQuery. fn. unwatch = function (id ){
Returnthis. each (function (){
ClearInterval ($ (this). data ('Watch _ timer '));
});
};

Create custom events
Copy codeThe Code is as follows:
JQuery. fn. valuechange = function (fn ){
Returnthis. bind ('valuechange', fn );
};
JQuery. event. special. valuechange = {
Setup: function (){
JQuery (this). watch ('value', function (){
JQuery. event. handle. call (this, {type: 'valuechange '});
});
},
Teardown: function (){
JQuery (this). unwatch ('value ');
}
};

It seems that this solution is perfect, but we will continue to check it later and find a more concise method. The Code is as follows:
Copy codeThe Code is as follows:
Var title = $ ("B. facility ");
Var title = $ ('# title'); // the element I want to monitor
Title. bind ('domainserted', function (e ){
Alert ('element now contains: '+ response (e.target).html ());
});

I think this should be code, do it! Fine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.