This time I will show you how to imitate the select tag of the ul-li tag of Vue.js, and what are the precautions for selecting the ul-li tag of Vue.js. The following is a practical case, let's take a look.
Goal: Use the ul-li tag combined with Vue.js knowledge to create a drop-down option list that imitates the select tag.
Knowledge points:
How to write and use components
Data transfer between components (use of props)
Data transfer between components (use of $emit)
Dynamic data binding (v-bind)
Custom event communication
Rendering:
1. Before any operation is performed, the drop-down list is hidden
2. Click the input box to display the drop-down list
3. Click the list item, and the input box value will change accordingly
PS: In order to demonstrate the binding of two sets of data data1 and data2, two lists are created in the example
html code:
nbsp;html> <meta> <title>ul-li模仿select下拉菜单</title> <link> <script></script> <p> <my-select></my-select> <my-select></my-select> </p>
JavaScript code
<script> //注册全局组件 //在my-select组件中套用ul-select组件,my-select为父组件ul-select为子组件 Vue.component('my-select', { //组件中data要写成函数形式 data() { return { ulShow: false, //默认ul不显示,单击input改变ul的显示状态 selectVal: '' //选项值,input的值与选项值动态绑定 } }, //父组件向子组件通信用props props: ['btnName', 'list'], template: ` <p id="selectWrap"> <p class="searchBox"> <input type="text" :value="selectVal" @click='ulShow = !ulShow'/> <a href="#" rel="external nofollow" class="search" v-text='btnName'> <my-ul v-show='ulShow' v-bind:list='list' v-on:receive='changeVal'> `, methods: { changeVal(value) { this.selectVal = value } } }) //子组件 Vue.component('my-ul', { props: ['list'], template: ` <ul class="skill"> <li v-for='item of list' v-on:click='selectLi(item)'>{{item}} `, methods: { selectLi: function(item) { //$emit触发当前实例上的自定义事件 receive this.$emit('receive', item); } } }) //创建Vue实例 new Vue({ el: '#demo', //定义两组数据分别传递到两个组件的li中,两个列表的操作互不影响 data: { data1: ['CSS', 'HTML', 'JavaScript'], data2: ['Vue.js', 'Node.js', 'Sass'], } }) </script>
CSS Style
ul, li { margin: 0; padding: 0; list-style: none; } #selectWrap { width: 250px; padding: 2rem; background: #4682b4; } .searchBox input, .searchBox a { line-height: 1.5rem; height: 1.5rem; margin-bottom: 1rem; padding: 0 5px; vertical-align: middle; border: 1px solid #aaa; border-radius: 5px; outline: none; } .searchBox a { display: inline-block; text-decoration: none; background-color: #b1d85c; } .skill li { font-size: 18px; line-height: 2rem; height: 2rem; padding-left: 5px; cursor: pointer; } .skill li:hover { background-color: #008b45; }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use Vue’s carousel component
The above is the detailed content of How to imitate the select tag of Vue.js's ul-li tag. For more information, please follow other related articles on the PHP Chinese website!

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
