How to beautify and modify the search box style by modifying JS files

PHPz
Release: 2023-04-21 14:55:17
Original
710 people have browsed it

DedeCMS is a very popular CMS website building system, which is widely favored for its user-friendliness, ease of use, scalability and open source. Among them, the search box style of DedeCMS is an integral part because it is easily noticed by users. However, the default search box style of DedeCMS seems a bit single and outdated, and does not meet the needs of modern websites. So how can we make the search box style of DedeCMS more in line with the needs of modern websites? This article came into being. This article will take DedeCMS 5.7SP2 stable version as an example to explain how to beautify and modify the search box style by modifying JS files.

  1. Locating JS files

The search function of DedeCMS is implemented in JavaScript, so we need to find the corresponding JS file and modify it. In DedeCMS, commonly used JS files are placed in the "/dede/js" directory. We can find the "search_guide.js" file in this directory. This is the JS file that needs to be modified.

  1. Backup JS file

Before modifying the JS file, we must first back up the original JS file to avoid errors due to misoperation. After copying the "search_guide.js" file, rename it to "search_guide_backup.js" as a backup file.

  1. Modify the JS file

After finding the "search_guide.js" file, open the file with a text editor and find the following code at the end of the file:

if($("#query").length>0) //搜索框表单ID
{
    $('#query').bind('focus',function(){
        if(this.value=='输入搜索内容') this.value='';
        if(this.style.backgroundColor=='#FFF3EA'||this.style.backgroundColor=='rgb(255, 243, 234)') this.style.backgroundColor='#FFFFFF';
    }).bind('blur',function(){
        if(this.value=='') this.value='输入搜索内容';
        if(this.style.backgroundColor=='#FFFFFF'||this.style.backgroundColor=='rgb(255, 255, 255)') this.style.backgroundColor='#FFF3EA';
    });
    $("#query").keyup(function(e){
        if(e.keyCode == 13)
        {
            window.location.href = ('/search.php?q='+encodeURIComponent(document.getElementById("query").value));
        }
    });
}
Copy after login

This code implements functions such as automatic clearing, automatic filling, and direct jump of the search box, but does not make any modifications to the style of the search box. We need to modify this code and add corresponding styles to beautify the search box.

The following is our modified code:

if($("#query").length>0) //搜索框表单ID
{
    $('#query').bind('focus',function(){
        if(this.value=='输入搜索内容') this.value='';
        if(this.style.backgroundColor=='#FFF3EA'||this.style.backgroundColor=='rgb(255, 243, 234)') this.style.backgroundColor='#FFFFFF';
        $(this).addClass('search_focus');
    }).bind('blur',function(){
        if(this.value=='') this.value='输入搜索内容';
        if(this.style.backgroundColor=='#FFFFFF'||this.style.backgroundColor=='rgb(255, 255, 255)') this.style.backgroundColor='#FFF3EA';
        $(this).removeClass('search_focus');
    });
    $("#query").keyup(function(e){
        if(e.keyCode == 13)
        {
            window.location.href = ('/search.php?q='+encodeURIComponent(document.getElementById("query").value));
        }
    });
}
Copy after login

We added the "addClass" and "removeClass" methods to the Bind function to add and remove class names to the search box. This class name We can use CSS to define the style of the search box. We can define two class names, one is the class name without focus, and the other is the class name with focus. The CSS style is as follows:

.search_box {
    width: 200px;
    height: 30px;
    background-color: #FFF3EA;
    border: none;
    font-size: 14px;
    color: #666666;
    padding-left: 8px;
    outline: none;
    -webkit-appearance: none;
}
.search_focus {
    background-color: #ffffff;
    box-shadow: 0px 1px 5px #ccc;
}
Copy after login

We define the basic style of the search box in the class name ".search_box", and define the style when the search box gets focus in the class name ".search_focus". In this way, we can modify and beautify the search box style by modifying these styles.

  1. Save the JS file

After completing the code modification, we need to save the JS file and replace the original file. We can upload the modified "search_guide.js" file to the "/dede/js" directory of the website through the FTP tool and replace the original file.

  1. Refresh the web page

We need to refresh the web page to make the modified code take effect. Open the website and click the search box. When the search box gains focus, the style of the search box changes, displaying the style defined by the class name ".search_focus". The style of the search box is no longer a single color, but has a more beautified and modern look.

Conclusion:

In this article, we modify the JavaScript file "search_guide.js" of DedeCMS to beautify the search box style. Of course, in addition to modifying JS files, we can also modify and beautify the style of the search box in other ways, such as using CSS style sheets. However, JavaScript is more convenient and powerful in most cases.
We hope this article can help you beautify and modify the search box style of DedeCMS, and make your website more beautiful and modern.

The above is the detailed content of How to beautify and modify the search box style by modifying JS files. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template