Using jQuery Correctly with Vue
Using jQuery in Vue has become a trend, because it allows us to manipulate DOM elements more conveniently and provides some practical tool functions. In this article, we will introduce in detail how to use jQuery in Vue and provide some examples to help you better understand it.
1. Introducing jQuery
Before using jQuery, we need to import it first. In Vue, we can import jQuery in the index.html file or in the main.js file. The following is the sample code:
index.html
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
main.js
import $ from 'jquery' Vue.prototype.$ = $
Both of these methods allow us to use jQuery in Vue, but the way of introducing it is different.
2. Using jQuery
After importing jQuery, we can use it in Vue. Here are some common uses:
1. Select the element
jQuery's selector makes it easy to select DOM elements. For example, if we want to select the element with the id app, we can write:
javascript Copy Codes
let app = $('#app')
2. Operation elements
jQuery methods can be used to easily manipulate DOM elements. For example, if we want to add a p tag to the element with the id app, we can write:
javascript Copy Codes
$('#app').append('<p>Hello World</p>')
3. Event Binding
jQuery's event binding makes it easy to bind events. For example, if we want to bind a click event to an element with the id app, we can write:
javascript Copy Codes
$('#app').on('click', function() { console.log('clicked') })
3. Examples in Vue
Below are some examples to help you better understand how to use jQuery with Vue.
1. Binding events
In Vue, we can use @click
binding events or jQuery's event binding. Here is the sample code:
<template>
<div id="app">
<button @click="add">Vue</button>
<button id="jquery">jQuery</button>
</div>
</template>
<script>
export default {
methods: {
add() {
console.log('Vue clicked')
}
},
mounted() {
$('#jquery').on('click', function() {
console.log('jQuery clicked')
})
}
}
</script>
In this example, we bind the click events of two buttons, one using Vue's @click and the other using jQuery's event binding.
2. Operation elements
In Vue, we can use ref
to get DOM elements, or we can use jQuery's selector to get DOM elements. The following is the sample code:
<template>
<div id="app">
<div ref="box"></div>
</div>
</template>
<script>
export default {
mounted() {
let box = $(this.$refs.box)
box.html('<p>Hello World</p>')
}
}
</script>
In this example, we use ref
to get the element with id box and use jQuery's html method to add a p tag to it.
3. Utility Functions
In Vue, we can use some utility functions to easily manipulate DOM elements, or we can use jQuery's utility functions. Here is the sample code:
<template>
<div id="app">
<input ref="input" type="text">
<button @click="focus">Focus</button>
</div>
</template>
<script>
export default {
methods: {
focus() {
$(this.$refs.input).focus()
}
}
}
</script>
In this example, we use ref
to get the input element, and use jQuery's focus method to give the input element focus.
Summarize
Using jQuery in Vue allows us to manipulate DOM elements more conveniently, and provides some practical tool functions. Before using jQuery, we need to import it first. jQuery's selector can easily select DOM elements, jQuery's method can conveniently manipulate DOM elements, and jQuery's event binding can conveniently bind events. In Vue, we can use ref
to get DOM elements, or we can use jQuery's selector to get DOM elements. Finally, we can also use jQuery's tool functions to conveniently manipulate DOM elements.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Configuring Apache Web Server on Ubuntu and Debian
Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM
-
This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:62 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to avoid soft 404 in the website during SEO
Publish Date:2025/03/17 Views:136 Category:NETWORK
-
This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand
Performance differences between HTTP REST API and WebSocket REST API
Publish Date:2025/03/17 Views:161 Category:NETWORK
-
What is a REST API? REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:201 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during