How to implement hidden element function in jquery
This article mainly introduces the jquery method of hiding element p by clicking on the blank space and the implementation code. Let’s learn together.
When we develop websites, we often click on the blank space to hide existing elements, p or other elements. This time I will teach you how to use jquery to achieve this special effect.
A jQuery special effect to hide the pop-up layer when clicking on the blank space. Click on the button to pop up the layer. Click on the blank space on the page to disappear the pop-up layer. JS code. The main function is to click the button to display the pop-up layer, and then click anywhere on the page to close the pop-up layer display effect. It is mainly the operation application of $(document).click. Demonstration Demo, effect source code:
CSS code:
* { padding: 0; margin: 0; } #btnShow{ margin: 100px auto 0; width: 90px; display: block; } #pTop{ border: 2px solid #666666; position: absolute; display: none; width: 400px; height: 200px; color: #333; background: #efefef; padding-top: 10px; text-align: center; font: 16px/30px "微软雅黑"; margin-top: -105px; margin-left: -200px; left: 50%; top: 50%; }
JS code:
$(function () { $('#btnShow').click(function (event) { //取消事件冒泡 event.stopPropagation(); //按钮的toggle,如果p是可见的,点击按钮切换为隐藏的;如果是隐藏的,切换为可见的。 $('#pTop').toggle('slow'); return false; }); //点击空白处隐藏弹出层,下面为滑动消失效果和淡出消失效果。 $(document).click(function(event){ var _con = $('#pTop'); // 设置目标区域 if(!_con.is(event.target) && _con.has(event.target).length === 0){ // Mark 1 //$('#pTop').slideUp('slow'); //滑动消失 $('#pTop').hide(1000); //淡出消失 } }); })
HTML code:
<body> <input type="button" id="btnShow" value="弹出框按钮" /> <p id="pTop"> 点击空白区域弹出层关闭! </p> </body>
After testing, when clicking on the blank space of the page on the mobile phone Iphone, the pop-up layer is closed and fails, and the document writing method is not supported. , Solution: You can add a background layer as a page blank object.
Click on the blank space to hide the pop-up layer Case 2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>点击空白处关闭弹出层</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> #box{width:300px;height:200px;border:1px solid #000;display:none;} .btn{color:red;} </style> <script type="text/javascript" src="http://www.internetke.com/public/js/jquery.js"></script> <script type="text/javascript"> $(function(){ $(".btn").click(function(event){ var e=window.event || event; if(e.stopPropagation){ e.stopPropagation(); }else{ e.cancelBubble = true; } $("#box").show(); }); $("#box").click(function(event){ var e=window.event || event; if(e.stopPropagation){ e.stopPropagation(); }else{ e.cancelBubble = true; } }); document.onclick = function(){ $("#box").hide(); }; }) </script> </head> <body> <p id="box"></p> <span class="btn">点击此处打开弹出层</span><br>点击空白处关闭弹出层 </body> </html>
Let me share with you the code of the method of hiding the p area by clicking outside the p area in js:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="js/jquery-1.4.4.min.js" ></script> <title></title> </head> <style type="text/css"> body { background-color:#999999; } #myp { background-color:#FFFFFF; width:250px; height:250px; display:none; } </style> <body> <input id="btn" type="button" value="显示p" /> <p id="myp"> This is a p; </p> </body> <script type="text/javascript"> var myp = $("#myp"); $(function () { $("#btn").click(function (event) { showp();//调用显示p方法 $(document).one("click", function () {//对document绑定一个影藏p方法 $(myp).hide(); }); event.stopPropagation();//阻止事件向上冒泡 }); $(myp).click(function (event) { event.stopPropagation();//阻止事件向上冒泡 }); }); function showp() { $(myp).fadeIn(); } </script>
The above is I compiled it for everyone, I hope it will be helpful to everyone in the future.
Related articles:
How to implement single-layer json sorting in key alphabetical order (detailed tutorial)
About cli usage in vue Absolute path reference problem
How to modify the view label background color in the WeChat applet
How to reset the display mode of vue print variables ( Detailed tutorial)
The above is the detailed content of How to implement hidden element function in jquery. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
