Object.defineProperty Understandinggrammar:
obj and prop are easy to understand. For example, we define a variable as const o = { name:'xbhog' } Among them, obj refers to o, and prop refers to o.name. Next, let's take a look at the descriptor.
Note: The value attribute, writable attribute and get attribute, set attribute in the descriptor are mutually exclusive. Only one can exist. Knowing the prerequisites, let's implement two-way binding of v-model in VueLet's look at the implementation code first: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Realize two-way data binding through js</title> </head> <body> <input type="text"/><br> <h1>Hello: <span>Update data</span></h1> <!-- Realize two-way data binding through js --> <script> // The method returns the first HTMLElement object in the document that matches the specified selector or selector group var ipt = document.querySelector('input'); var p = document.querySelector('span'); var data = {name:""}; /* The oninput event is triggered when the user types something. This event is triggered when the value of an <input> or <textarea> element changes. */ ipt.oninput = function(){ // Pass the value in ipt.value to the value of data.name data.name = ipt.value; } //Hijack ipt.value Object.defineProperty(data,"name",{ //Data subscription get(){ return ipt.value; //The get method will be called when accessing}, //Data hijacking //name:value set(value) { p.innerHTML = value; ipt.value = value; } }) </script> </body> </html> First, we use document.querySelector to get the Html objects of the input and span tags, and then define a data object with the attribute name empty. Use the oninput event listener to monitor user input (this event is triggered when the value of an <input> or <textarea> element changes). Pass the value in ipt.value to the value of data.name; data.name = ipt.value; Use Object.defineProperty to hijack user input data.
Object.defineProperty(data,"name",{ //Data subscription get(){ return ipt.value; //When accessing data.name, the get method will be called to call ipt.value to get the current value}, // Data hijacking set(value) { // The set method will be automatically called when setting data p.innerHTML = value; ipt.value = value; } The effect is more obvious: Set method: Get method: The final effect: References:
SummarizeThis is the end of this article about using js to implement the two-way data binding function in Vue2.0. For more relevant content about using js to implement Vue2 two-way binding, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The MySql 8.0.16 version installation prompts that "UTF8B4" is used instead of "UTF8B3"
>>: Implementation of debugging code through nginx reverse proxy
Use CSS to prevent Lightbox to realize the displa...
The creation of the simplest hello world output i...
MySQL has the following logs: Error log: -log-err...
I typed a wrong mysql command and want to cancel ...
If we want to make a carousel, we must first unde...
One of our web projects has seen an increase in t...
This article shares the specific code of js to re...
Without further ado Start recording docker pullin...
1. Introduction to Docker Docker is developed in ...
Table of contents Basic usage of Promise: 1. Crea...
Table of contents Install Docker on CentOS 8 1. U...
Preface Linux's file permission management is...
Preface After MySQL version 3.23.44, InnoDB engin...
Create Table create table table name create table...
I recently added a very simple color scheme (them...