How to use Vue to control the number of characters and bytes displayed
This time I will show you how to use Vue to control the number of characters and bytes displayed, and how to use Vue to control the number of characters and bytes displayed. What are the precautions?The following is a practical case, let's take a look take a look.
Requirements
Requirements: Combined with Vue to achieve the following effect
Maximum input in the input box 16 characters
Chinese characters can be displayed at most 5, and the excess characters are displayed with
...
English characters can be displayed at most 10 , the excess part is displayed with
...
to achieve
construction Simple page, and set a simple style
Before officially starting to write the core code, you must first set up the code structure, so that it will look simpler when you write it later.
Firstly, an input box is needed to input content, and secondly, an element is needed to display the content in the input box to achieve two-way binding of data.
Among them, the maximum length of the input content can be specified through the attributes of the input tag.
<p> <input> </p><p> </p><p>内容:<span>{{txt}}</span></p> <p>输入的字符个数:<span>{{computedCharLen}}</span></p> <p>输入的字节个数:<span>{{computedByteLen}}</span></p>
The structure of the page has been completed, so the next step is to do some simple style optimization.
* { margin: 0; padding: 0; -webkit-box-sizing: border-box; box-sizing: border-box; } body { background: #efefef; } .clsinp { width: 100%; height: 40px; outline: none; line-height: 40px; font-size: 16px; padding: 0 10px; margin-top: 20px; color: blue; } .clsmsg { padding: 10px 10px; } .clsmsg span { color: blue; }
The last step should be to introduce Vue and then build some simple data content.
var vm = new Vue({ el: '#app', data() { return { txt: '' } }, // 后期代码在下面补充 })
ASCII within and outside the range
To understand the content of ASCII, please go tohttp://www.asciima .com/
.
ASCII contains 256 characters, so characters beyond 256 are all non-ASCII characters. Generally, Chinese characters are within this range.
Therefore, characters whose encoding is not 0-255 can be matched using the regular expression /[^\x00-\xff]/g
. At this time, an idea is provided. If it is not a character in the ASCII code, then it will occupy two bytes by default.
We modify the page structure and output some test information:
<p> <input> </p><p> </p><p>内容:<span>{{txt}}</span></p> <p>输入的字符个数:<span>{{computedCharLen}}</span></p> <p>输入的字节个数:<span>{{computedByteLen}}</span></p>
Supplement the required calculated attributes:
computed: { // 获取字符的个数 computedCharLen() { return this.txt.length }, // 获取字节的个数 computedByteLen() { return this.txt.replace(/[^\x00-\xff]/g, '01').length } }
At this time, we enter the content and the following effect appears:
At this time, you will find that it has been implemented. The ASCII code range occupies 1 digit, and the ASCII code outside the range occupies 2 digits.
Control the displayed content
Content display is implemented using calculated properties:
<p>内容:<span>{{computedTxt}}</span></p>
// 控制显示的内容 computedTxt() { return this.methodGetByteLen(this.txt, 10) }
The following is addedmethodGetByteLen
Method:
/** * str 需要控制的字符串 * len 字节的长度,如5个汉字,10个英文,输入参数就是10 */ methodGetByteLen(str, len) { // 如果字节的长度小于控制的长度,那么直接返回 if (this.computedByteLen = len) { // Math.floor(i / 2) * 这里是控制特殊情况的显示 // 如 '一二aaa一二三四',显示的结果就是 '一二aaa一...' return str.substr(0, Math.floor(i / 2) * 2) + '...' } } }
The final display does not exceed the maximum specified length
Exceeds the maximum specified length (Chinese character input)
Exceeds the maximum specified length (numeric input)
Exceeds the maximum specified length (combination of Chinese characters and letters)
Complete code
Finally, paste the final code:
nbsp;html>Document
<p>内容:<span>{{computedTxt}}</span></p>
输入的字符个数:{{computedCharLen}}
输入的字节个数:{{computedByteLen}}
<script></script> <script> var vm = new Vue({ el: '#app', data() { return { txt: '' } }, // 后期代码在下面补充 methods: { methodGetByteLen(str, len) { if (this.computedByteLen <= len) { return str } for (let i = Math.floor(len / 2); i < str.length; i++) { if (str.substr(0, i).replace(/[^\x00-\xff]/g, '01').length >= len) { return str.substr(0, Math.floor(i / 2) * 2) + '...' } } } }, computed: { // 获取字符的个数 computedCharLen() { return this.txt.length }, // 获取字节的个数 computedByteLen() { return this.txt.replace(/[^\x00-\xff]/g, '01').length }, // 控制显示的内容 computedTxt() { return this.methodGetByteLen(this.txt, 10) } } }) </script>
I believe you have mastered the method after reading the case in this article, For more exciting content, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to implement a custom multi-selection event for WeChat applet
How to create-react- The app is modified to support multiple pages
The above is the detailed content of How to use Vue to control the number of characters and bytes displayed. For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools