js implements a simple English-Chinese dictionary

js implements a simple English-Chinese dictionary

This article shares the specific code of js to implement a simple English-Chinese dictionary for your reference. The specific content is as follows

1. Objectives

Use js to implement a simple English-Chinese dictionary query function to achieve the effect of single word search

2. Implementation steps

1. Use a js file to save all the words in the English-Chinese dictionary as string format

2. In another js file or html page script, separate the string of the file containing the vocabulary into an array, add the Map method, and traverse it

3. Match the contents of the search box with the array elements and return the results

3. Code Module

1.html part

<div id="div1">
     <input id='word' type="text" placeholder="Enter English word" />
     <div id='desc'></div>
</div>

2.css part

#div1 {
            width: 200px;
            height: 200px;
            padding: 50px;
            background-color: lightgray;
            border: 1px solid black;
            margin: 100px auto
        }
 
        #word {
            width: 200px;
            height: 30px;
            font-size: 18px;
        }
 
        #desc {
            width: 200px;
            height: 150px;
            margin-top: 20px;
            background-color: lightgreen
        }

3.js part

 <script src='demo.js'></script>
    <script>
        // Split the string into an array by line breaks let arr = word.split("\n");
 
        // Create Map method let map = new Map();
 
        // Traverse the array for (var i = 0; i < arr.length - 1; i += 2) {
            map.set(arr[i].substring(1), arr[i + 1].substring(6));
        }
 
        window.onload = function () {
            let oWord = document.getElementById("word");
            let oDesc = document.getElementById("desc");
 
            oWord.onkeyup = function () {
                let value = map.get(this.value);
                if (value) {
                    oDesc.innerHTML = value;
                } else {
                    oDesc.innerHTML = "No such word found";
                }
            }
        }
</script>

4.js external link vocabulary string fragment screenshot

4. Rendering

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • js and html5 realize the mobile scratch card lottery effect, perfect compatibility with android/IOS
  • Analysis of js roulette lottery example
  • js lottery to achieve random lottery code effect
  • jquery.rotate.js implements the lottery code of the turntable with optional lottery number and winning content
  • js simple lottery code
  • js implements a simple random lottery method
  • js to achieve a large turntable lottery game example
  • Native JS realizes the effect of nine-square lottery
  • JavaScript random lottery program code
  • JS simulation lottery sequence effect implementation code

<<:  Detailed tutorial on installing MariaDB on CentOS 8

>>:  A summary of the knowledge points of database indexing. Everything you need to know is here.

Recommend

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...

Example code for implementing the wavy water ball effect using CSS

Today I learned a new CSS special effect, the wav...

Why MySQL does not recommend using subqueries and joins

To do a paginated query: 1. For MySQL, it is not ...

Specific implementation methods of MySQL table sharding and partitioning

Vertical table Vertical table splitting means spl...

js to achieve simple drag effect

This article shares the specific code of js to ac...

How to use mysqldump for full and point-in-time backups

Mysqldump is used for logical backup in MySQL. Al...

Summary of MySQL development standards and usage skills

1. Naming conventions 1. Database names, table na...

Detailed explanation of MySQL transaction isolation level and MVCC

Table of contents Transaction Isolation Level Pro...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands

What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...

How to design and optimize MySQL indexes

Table of contents What is an index? Leftmost pref...

Introduction to SSL certificate installation and deployment steps under Nginx

Table of contents Problem description: Installati...

MySQL 8.0.21 installation steps and problem solutions

Download the official website First go to the off...