Method and device for automatically closing pull-down selection box and electronic equipment

By creating DOM elements and event listeners, the display of the dropdown selection box is automatically controlled, solving the problem of cumbersome operation caused by users manually closing it, and improving user experience and efficiency.

CN118897639AInactive Publication Date: 2024-11-05ONE NETWORK INTEROPERABILITY (BEIJING) TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411353624.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-26
Publication Date
2024-11-05
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In existing technologies, drop-down selection boxes require users to manually close them, which is cumbersome and affects user efficiency.

Method used

By creating three DOM elements—wrap, valueWrap, and dropDwonWrap—and using JavaScript event listeners and CSS positioning, the display of the dropdown selection box can be automatically controlled, including preventing event bubbling and determining the mouse click position to achieve automatic closing.

Benefits of technology

It improves the efficiency of drop-down selection boxes, simplifies user interaction, and reduces unnecessary operation steps.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118897639A_ABST
    Figure CN118897639A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a method and device for automatically closing a pull-down selection box and electronic equipment, and belongs to the technical field of data processing.The method comprises the steps that three dom elements of a trap, a value Wtrap and a dropDwonWtrap are created; the method comprises the following steps: defining a variable Visible to control whether a dropDwonWrap element is displayed or not, wherein the value of the variable Visible is true or false; a click event is added to the trap element, and bubbling of the click event is prevented through e.stop Propagation () in the click event; a click event is added to the value Wrap element, and when a user clicks the value Wrap element, the value of the visible is updated to be! Visible; in a DocumentClickFun function, a dom element of a current mouse clicking position of a user is obtained through e.target, whether a trap element contains the e.target or not is judged through a contains method in javascript, and if the trap element does not contain the e.target, the value of vision is updated to be false, so that closing of a pull-down selection box is achieved. According to the method and the device, the selection box pull-down operation efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data processing technology, and in particular to a method, device and electronic device for automatically closing a drop-down selection box. Background Art

[0002] The drop-down select box is a common user interface element, widely used in web pages and desktop applications. It mainly consists of two parts: a text box (or button) that displays the current option, and a list containing multiple options. When the user clicks the text box or button, the list will appear in the form of a drop-down menu for the user to select an option from.

[0003] The main features of the drop-down selection box include:

[0004] Save space: Normally only the currently selected item is displayed, and the drop-down list is expanded when the user needs to make a selection, which can effectively utilize the limited screen space.

[0005] Easy to organize a large number of options: When there are a large number of options, the drop-down selection box can hide these options and only display them when needed, making the interface more concise.

[0006] Convenient selection: Users can select different options by clicking or keyboard navigation, which improves the convenience of interaction.

[0007] Scalability: Options can be increased or decreased as needed, with high flexibility.

[0008] In project development, drop-down selection boxes are quite common. When the user completes the operation in the drop-down box, the user needs to manually close the drop-down selection box. For projects with a large number of drop-down selection boxes, frequent opening or closing of the drop-down selection boxes will affect the user's operating efficiency of the drop-down selection boxes, resulting in cumbersome operations.

[0009] The above problems become technical problems that need to be solved. Summary of the invention

[0010] In view of this, an embodiment of the present invention provides a method, device and electronic device for automatically closing a drop-down selection box, which at least partially solve the problems existing in the prior art.

[0011] In a first aspect, an embodiment of the present invention provides a method for automatically closing a drop-down selection box, comprising:

[0012] Create three DOM elements: wrap, valueWrap and dropDwonWrap. The valueWrap element and the dropDwonWrap element are at the same level. The valueWrap element and the dropDwonWrap element are placed in the wrap element. The dropDwonWrap element uses position:absolute to control its display position.

[0013] Define the variable visible to control whether the dropDwonWrap element is displayed or not. The value of the variable visible is true or false.

[0014] Add a click event to the wrap element, and use e.stopPropagation() in the click event to stop the bubbling of the click event so that the click event in the wrap element does not affect elements outside the wrap element;

[0015] Add a click event to the valueWrap element. When the user clicks the valueWrap element, update the value of visible to !visible.

[0016] Add a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun). In the DocumentClickFun function, use e.target to get the DOM element where the user's current mouse click is located. Use the contains method in javascript to determine whether the wrap element contains e.target. If not, update the value of visible to false to close the drop-down selection box.

[0017] According to a specific implementation of the embodiment of the present disclosure, the creation of three DOM elements, wrap, valueWrap and dropDwonWrap, includes:

[0018] Create a parent container element wrap, which is used to contain child elements and acts as a container for click events to prevent event bubbling;

[0019] Create a child element valueWrap inside the wrap element. The valueWrap element allows users to interact with it. Clicking the valueWrap element changes the state of the drop-down menu.

[0020] According to a specific implementation of the embodiment of the present disclosure, the creation of three DOM elements, wrap, valueWrap and dropDwonWrap, also includes:

[0021] Create a child element dropDwonWrap inside the wrap element. The dropDwonWrap element contains the contents of the drop-down list. The dropDwonWrap element determines its position relative to the parent element through the position: absolute attribute in CSS.

[0022] According to a specific implementation of an embodiment of the present disclosure, defining a variable visible to control whether the dropDwonWrap element is displayed includes:

[0023] Define the variable visible to track the display status of the dropDwonWrap element;

[0024] Initialize the visible variable to false. Each time the value of the visible variable changes, check the new value of the visible variable and update the CSS style of the dropDwonWrap element accordingly.

[0025] According to a specific implementation of an embodiment of the present disclosure, adding a click event to the wrap element and stopping the bubbling of the click event by using e.stopPropagation() in the click event so that the click event in the wrap element does not affect elements outside the wrap element include:

[0026] Use JavaScript to add a click event listener to the wrap element. Inside the click event listener, when a click event is detected, get the event object e;

[0027] Use the stopPropagation() method on the event object e to stop the click event from propagating further upward.

[0028] According to a specific implementation of the embodiment of the present disclosure, adding a click event to the valueWrap element, when the user clicks the valueWrap element, updating the value of visible to !visible, includes:

[0029] When the user clicks the valueWrap element, update the value of visible to its logical negation.

[0030] According to a specific implementation of the embodiment of the present disclosure, the click event DocumentClickFun is added to the document object through document.addEventListener('click', DocumentClickFun), the DOM element of the user's current mouse click position is obtained through e.target in the DocumentClickFun function, and the contains method in javascript is used to determine whether the wrap element contains e.target, including:

[0031] In the click event listener's processing function DocumentClickFun, the DOM element clicked by the user is obtained through the target attribute of the event object e;

[0032] Use the contains method to check whether the wrap element contains the clicked target element. If the wrap element directly contains e.target or contains e.target through a descendant relationship, it returns true; otherwise, it returns false;

[0033] If the clicked target element is not within the wrap element, set the visible variable to false, thereby hiding the dropDwonWrap element.

[0034] According to a specific implementation of the embodiment of the present disclosure, the click event DocumentClickFun is added to the document object through document.addEventListener('click', DocumentClickFun), the DOM element of the user's current mouse click position is obtained through e.target in the DocumentClickFun function, and the contains method in javascript is used to determine whether the wrap element contains e.target, and further includes:

[0035] Get a reference to the wrap element through document.getElementById('wrap');

[0036] Define the DocumentClickFun function, which is called when a click event occurs on the document;

[0037] Inside the DocumentClickFun function, the clicked DOM element is obtained through the target property of the event object e.

[0038] In a second aspect, an embodiment of the present invention provides a device for automatically closing a drop-down selection box, comprising:

[0039] Create a module, create three DOM elements: wrap, valueWrap and dropDwonWrap. The valueWrap element and the dropDwonWrap element are at the same level. The valueWrap element and the dropDwonWrap element are placed in the wrap element. The dropDwonWrap element uses position:absolute to control its display position.

[0040] Define the module and define the variable visible to control whether the dropDwonWrap element is displayed or not. The value of the variable visible is true or false.

[0041] Add a module to add a click event to the wrap element. In the click event, use e.stopPropagation() to stop the bubbling of the click event so that the click event in the wrap element does not affect elements outside the wrap element.

[0042] Update the module and add a click event to the valueWrap element. When the user clicks the valueWrap element, update the value of visible to !visible.

[0043] Close the module, add a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun), get the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and use the contains method in javascript to determine whether the wrap element contains e.target. If not, update the value of visible to false to close the drop-down selection box.

[0044] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:

[0045] at least one processor; and,

[0046] a memory communicatively connected to the at least one processor; wherein,

[0047] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method for automatically closing the drop-down selection box in any of the first aspects or any of the implementations of the first aspect.

[0048] In a fourth aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the method of automatically closing the drop-down selection box in the aforementioned first aspect or any implementation of the first aspect.

[0049] In the fifth aspect, an embodiment of the present invention further provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method of automatically closing the drop-down selection box in the aforementioned first aspect or any implementation of the first aspect.

[0050] The solution for automatically closing the drop-down selection box in the embodiment of the present invention includes: creating three DOM elements, wrap, valueWrap and dropDwonWrap, wherein the valueWrap element and the dropDwonWrap element are at the same level, the valueWrap element and the dropDwonWrap element are placed in the wrap element, and the dropDwonWrap element controls its display position through position:absolute; defining a variable visible to control whether the dropDwonWrap element is displayed or not, and the value of the variable visible is true or false; adding a click event to the wrap element, and in the click event, using e.stopPropagation() to stop the bubbling of the click event, so that the click event in the wrap element does not affect the elements outside the wrap element; adding a click event to the valueWrap element, and when the user clicks the valueWrap element, updating the value of visible to !visible; and calling document.addEventListener('click', DocumentClickFun) adds a click event DocumentClickFun to the document object, obtains the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and determines whether the wrap element contains e.target through the contains method in javascript. If not, the value of visible is updated to false to close the drop-down selection box. The solution of the present application can improve the operating efficiency of the drop-down selection box. BRIEF DESCRIPTION OF THE DRAWINGS

[0051] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0052] Figure 1 A schematic flow chart of a method for automatically closing a drop-down selection box provided by an embodiment of the present invention;

[0053] Figure 2 A schematic flow chart of another method for automatically closing a drop-down selection box provided by an embodiment of the present invention;

[0054] Figure 3 A schematic diagram of the structure of a device for automatically closing a drop-down selection box provided by an embodiment of the present invention;

[0055] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0056] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.

[0057] The following describes the embodiments of the present disclosure through specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all of the embodiments. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present disclosure.

[0058] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.

[0059] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present disclosure. The drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.

[0060] Additionally, in the following description, specific details are provided to facilitate a thorough understanding of the examples. However, it will be understood by those skilled in the art that the aspects described may be practiced without these specific details.

[0061] The embodiment of the present disclosure provides a method for automatically closing a drop-down selection box. The method for automatically closing a drop-down selection box provided in this embodiment can be executed by a computing device, which can be implemented as software, or as a combination of software and hardware, and the computing device can be integrated in a server, terminal device, etc.

[0062] See also Figure 1 , the embodiment of the present disclosure provides a method for automatically closing a drop-down selection box, comprising:

[0063] S101, create three DOM elements, wrap, valueWrap and dropDwonWrap, the valueWrap element and the dropDwonWrap element are at the same level, the valueWrap element and the dropDwonWrap element are placed in the wrap element, and the dropDwonWrap element controls its display position through position:absolute.

[0064] You can create a div element as a container and give it a unique ID, such as id="wrap". Create another div element inside the wrap element and assign it an ID, such as id="valueWrap". This element will be used to display the currently selected value or as a button for the user to trigger the drop-down list.

[0065] Also inside the wrap element, create another div element and assign it an ID, such as id="dropDwonWrap". This element will contain the contents of the drop-down list and will be positioned using position:absolute; in the CSS style.

[0066] You can use CSS style to set the position property of dropDwonWrap to absolute, and adjust its top, left and other properties as needed to display it in the appropriate position.

[0067] Through the above steps, a basic drop-down selection box structure can be created. The wrap element serves as the container of the entire component, the valueWrap element is used to trigger the display / hide of the drop-down list, and the dropDwonWrap element is the actual content of the drop-down list. Through absolute positioning, dropDwonWrap can appear in the correct position when the user clicks valueWrap.

[0068] S102, define a variable visible to control whether the dropDwonWrap element is displayed or not, and the value of the variable visible is true or false.

[0069] You can create a Boolean variable visible with its initial value set to false, indicating that the dropDwonWrap element is hidden by default.

[0070] When the user interacts with the valueWrap element (such as clicking), update the value of the visible variable. If visible is currently false, set it to true; vice versa. Doing this can toggle the display state through simple logical operations.

[0071] Each time the value of visible changes, check its latest value. If visible is true, show the dropDwonWrap element; if it is false, hide the element.

[0072] In this way, the visible variable becomes the key to control the display state of dropDwonWrap. When visible is true, dropDwonWrap is visible; when visible is false, dropDwonWrap is invisible.

[0073] S103, adding a click event to the wrap element, and stopping the bubbling of the click event through e.stopPropagation() in the click event, so that the click event in the wrap element does not affect elements outside the wrap element.

[0074] You can add a click event listener to the wrap element by getting a reference to the wrap element and calling the addEventListener method. Inside the event handler, first get the event object e. Inside the event handler, call the stopPropagation() method on the event object. This stops the event from propagating further to parent elements, thus preventing the event handlers on those parent elements from being executed.

[0075] By following the above steps, you can ensure that when a click occurs inside a wrap element, it will not trigger any event handlers attached to the parent element above the wrap element. You can precisely control the scope of the event and avoid unnecessary side effects.

[0076] When the user clicks the wrap element, the event handler is called and the stopPropagation() method is used to stop the event from bubbling upwards. In this way, even if the parent element of the wrap also has click event listeners, these listeners will not be triggered when the user clicks the wrap. This is very useful for application scenarios where clicks need to be processed in a specific area without interfering with the global or other areas.

[0077] S104, adding a click event to the valueWrap element. When the user clicks the valueWrap element, the value of visible is updated to !visible.

[0078] You can add a click event listener to the valueWrap element by getting a reference to the valueWrap element and calling the addEventListener method.

[0079] Inside the event handling function, first get the current value of visible. Invert the value of visible (that is, if it is currently true, change it to false; if it is false, change it to true). Update the UI according to the new value of visible. If visible is true, display the dropDwonWrap element; if it is false, hide the element.

[0080] In this way, every time the user clicks valueWrap, the value of visible will switch between true and false, thereby controlling the display and hiding of the dropDwonWrap element.

[0081] S105, add a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun), obtain the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and determine whether the wrap element contains e.target through the contains method in javascript. If not, update the value of visible to false to close the drop-down selection box.

[0082] You can get a reference to the wrap element by using document.getElementById('wrap'). At the same time, define a function DocumentClickFun, which will be called when a click event occurs on the document.

[0083] Inside the DocumentClickFun function, get the clicked DOM element through the target property of the event object e. Use the contains method of the wrap element to check whether the wrap element contains the clicked element e.target.

[0084] If e.target is not within the wrap element, it means that the click occurred outside the wrap element. At this time, you can update the visible variable to false to close the dropDwonWrap element.

[0085] This way, you can ensure that the dropDwonWrap element is closed when the user clicks outside the wrap element. This implements a common interaction method for drop-down selection boxes, that is, the drop-down menu will remain open only when clicking inside the wrap element; and the drop-down menu will be closed when clicking outside the wrap.

[0086] According to a specific implementation of the embodiment of the present disclosure, the creation of three DOM elements, wrap, valueWrap and dropDwonWrap, includes:

[0087] Create a parent container element wrap, which is used to contain child elements and acts as a container for click events to prevent event bubbling;

[0088] Create a child element valueWrap inside the wrap element. The valueWrap element allows users to interact with it. Clicking the valueWrap element changes the state of the drop-down menu.

[0089] According to a specific implementation of the embodiment of the present disclosure, the creation of three DOM elements, wrap, valueWrap and dropDwonWrap, also includes:

[0090] Create a child element dropDwonWrap inside the wrap element. The dropDwonWrap element contains the contents of the drop-down list. The dropDwonWrap element determines its position relative to the parent element through the position: absolute attribute in CSS.

[0091] According to a specific implementation of an embodiment of the present disclosure, defining a variable visible to control whether the dropDwonWrap element is displayed includes:

[0092] Define the variable visible to track the display status of the dropDwonWrap element;

[0093] Initialize the visible variable to false. Each time the value of the visible variable changes, check the new value of the visible variable and update the CSS style of the dropDwonWrap element accordingly.

[0094] According to a specific implementation of an embodiment of the present disclosure, adding a click event to the wrap element and stopping the bubbling of the click event by using e.stopPropagation() in the click event so that the click event in the wrap element does not affect elements outside the wrap element include:

[0095] Use JavaScript to add a click event listener to the wrap element. Inside the click event listener, when a click event is detected, get the event object e;

[0096] Use the stopPropagation() method on the event object e to stop the click event from propagating further upward.

[0097] According to a specific implementation of the embodiment of the present disclosure, adding a click event to the valueWrap element, when the user clicks the valueWrap element, updating the value of visible to !visible, includes:

[0098] When the user clicks the valueWrap element, update the value of visible to its logical negation.

[0099] According to a specific implementation of the embodiment of the present disclosure, the click event DocumentClickFun is added to the document object through document.addEventListener('click', DocumentClickFun), the DOM element of the user's current mouse click position is obtained through e.target in the DocumentClickFun function, and the contains method in javascript is used to determine whether the wrap element contains e.target, including:

[0100] In the click event listener's processing function DocumentClickFun, the DOM element clicked by the user is obtained through the target attribute of the event object e;

[0101] Use the contains method to check whether the wrap element contains the clicked target element. If the wrap element directly contains e.target or contains e.target through a descendant relationship, it returns true; otherwise, it returns false;

[0102] If the clicked target element is not within the wrap element, set the visible variable to false, thereby hiding the dropDwonWrap element.

[0103] According to a specific implementation of the embodiment of the present disclosure, the click event DocumentClickFun is added to the document object through document.addEventListener('click', DocumentClickFun), the DOM element of the user's current mouse click position is obtained through e.target in the DocumentClickFun function, and the contains method in javascript is used to determine whether the wrap element contains e.target, and further includes:

[0104] Get a reference to the wrap element through document.getElementById('wrap');

[0105] Define the DocumentClickFun function, which is called when a click event occurs on the document;

[0106] Inside the DocumentClickFun function, the clicked DOM element is obtained through the target property of the event object e.

[0107] Corresponding to the above method embodiment, see Figure 3 The embodiment of the present invention further discloses a device 30 for automatically closing a drop-down selection box, comprising:

[0108] Create module 301, create three DOM elements, wrap, valueWrap and dropDwonWrap, the valueWrap element and the dropDwonWrap element are at the same level, the valueWrap element and the dropDwonWrap element are placed in the wrap element, and the dropDwonWrap element uses position:absolute to control its display position;

[0109] Definition module 302, defining a variable visible to control whether the dropDwonWrap element is displayed or not, and the value of the variable visible is true or false;

[0110] Add module 303, add a click event to the wrap element, and use e.stopPropagation() in the click event to stop the bubbling of the click event, so that the click event in the wrap element does not affect the elements outside the wrap element;

[0111] Update module 304, adding a click event to the valueWrap element, when the user clicks the valueWrap element, updates the value of visible to !visible;

[0112] Close module 305, add a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun), get the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and use the contains method in javascript to determine whether the wrap element contains e.target. If not, update the value of visible to false to close the drop-down selection box.

[0113] See also Figure 4 The embodiment of the present invention further provides an electronic device 60, the electronic device comprising:

[0114] at least one processor; and,

[0115] a memory communicatively connected to the at least one processor; wherein,

[0116] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for automatically closing the drop-down selection box in the aforementioned method embodiment.

[0117] An embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the aforementioned method embodiment.

[0118] An embodiment of the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method of automatically closing the drop-down selection box in the aforementioned method embodiment.

[0119] Reference below Figure 4 , which shows a schematic diagram of the structure of an electronic device 60 suitable for implementing the embodiment of the present disclosure. The electronic device in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 4 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0120] like Figure 4 As shown, the electronic device 60 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 60 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0121] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, a gage, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 60 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 4 The electronic device 60 is shown with various devices, but it should be understood that it is not required to implement or have all the devices shown. More or fewer devices may be implemented or have instead.

[0122] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.

[0123] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims

1. A method for automatically closing a drop-down selection box. It is characterized in that include: Create three DOM elements: wrap, valueWrap and dropDwonWrap. The valueWrap element and the dropDwonWrap element are at the same level. The valueWrap element and the dropDwonWrap element are placed in the wrap element. The dropDwonWrap element uses position:absolute to control its display position. Define the variable visible to control whether the dropDwonWrap element is displayed or not. The value of the variable visible is true or false. Add a click event to the wrap element, and use e.stopPropagation() in the click event to stop the bubbling of the click event so that the click event in the wrap element does not affect elements outside the wrap element; Add a click event to the valueWrap element. When the user clicks the valueWrap element, update the value of visible to !visible. Add a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun). In the DocumentClickFun function, use e.target to get the DOM element where the user's current mouse click is located. Use the contains method in javascript to determine whether the wrap element contains e.target. If not, update the value of visible to false to close the drop-down selection box.

2. The method according to claim 1, It is characterized in that The creation of three DOM elements, wrap, valueWrap and dropDwonWrap, includes: Create a parent container element wrap, which is used to contain child elements and acts as a container for click events to prevent event bubbling; Create a child element valueWrap inside the wrap element. The valueWrap element allows users to interact with it. Clicking the valueWrap element changes the state of the drop-down menu.

3. The method according to claim 2, It is characterized in that The creation of three DOM elements, wrap, valueWrap and dropDwonWrap, also includes: Create a child element dropDwonWrap inside the wrap element. The dropDwonWrap element contains the contents of the drop-down list. The dropDwonWrap element determines its position relative to the parent element through the position: absolute attribute in CSS.

4. The method according to claim 3, It is characterized in that The definition of the variable visible to control whether the dropDwonWrap element is displayed or not includes: Define the variable visible to track the display status of the dropDwonWrap element; Initialize the visible variable to false. Each time the value of the visible variable changes, check the new value of the visible variable and update the CSS style of the dropDwonWrap element accordingly.

5. The method according to claim 4, It is characterized in that The method of adding a click event to the wrap element and stopping the bubbling of the click event by calling e.stopPropagation() in the click event so that the click event in the wrap element does not affect elements outside the wrap element includes: Use JavaScript to add a click event listener to the wrap element. Inside the click event listener, when a click event is detected, get the event object e; Use the stopPropagation() method on the event object e to stop the click event from propagating further upward.

6. The method according to claim 5, It is characterized in that The method of adding a click event to the valueWrap element and updating the value of visible to !visible when the user clicks the valueWrap element includes: When the user clicks the valueWrap element, update the value of visible to its logical negation.

7. The method according to claim 6, It is characterized in that The method adds a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun), obtains the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and determines whether the wrap element contains e.target through the contains method in javascript, including: In the click event listener's processing function DocumentClickFun, the DOM element clicked by the user is obtained through the target attribute of the event object e; Use the contains method to check whether the wrap element contains the clicked target element. If the wrap element directly contains e.target or contains e.target through a descendant relationship, it returns true; otherwise, it returns false; If the clicked target element is not within the wrap element, set the visible variable to false, thereby hiding the dropDwonWrap element.

8. The method according to claim 7, It is characterized in that The method adds a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun), obtains the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and determines whether the wrap element contains e.target through the contains method in javascript, and also includes: Get a reference to the wrap element through document.getElementById('wrap'); Define the DocumentClickFun function, which is called when a click event occurs on the document; Inside the DocumentClickFun function, the clicked DOM element is obtained through the target property of the event object e.

9. A device for automatically closing a drop-down selection box. It is characterized in that include: Create a module, create three DOM elements: wrap, valueWrap and dropDwonWrap. The valueWrap element and the dropDwonWrap element are at the same level. The valueWrap element and the dropDwonWrap element are placed in the wrap element. The dropDwonWrap element uses position:absolute to control its display position. Define the module and define the variable visible to control whether the dropDwonWrap element is displayed or not. The value of the variable visible is true or false. Add a module to add a click event to the wrap element. In the click event, use e.stopPropagation() to stop the bubbling of the click event so that the click event in the wrap element does not affect elements outside the wrap element. Update the module and add a click event to the valueWrap element. When the user clicks the valueWrap element, update the value of visible to !visible. Close the module, add a click event DocumentClickFun to the document object through document.addEventListener('click', DocumentClickFun), get the DOM element of the user's current mouse click position through e.target in the DocumentClickFun function, and use the contains method in javascript to determine whether the wrap element contains e.target. If not, update the value of visible to false to close the drop-down selection box.

10. An electronic device, It is characterized in that The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method for automatically closing a drop-down selection box as described in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Dropdown display method and device

    CN108733279A

  • Method for selectively preventing event transfer based on parameter event pointing feature during browser DOM event transfer and application

    CN114356444A