Method: 1. Use the "#id" selector to obtain the element object with the specified id, the syntax is "$("#specified id value")"; 2. Use the hide() method or toggle() The method can hide the element object with the specified id. The syntax is "element object.hide()" or "element object.toggle()".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How jquery hides divs based on id
1. Use the id selector to obtain elements
#The id selector selects elements with the specified id .
id refers to the id attribute of the HTML element.
Note: The id attribute must be unique within the document.
Note: Do not use id attributes starting with numbers! This may cause problems in some browsers.
Syntax
$("#id")
2. Hide elements
hide() method hides the selected elements.
Tip: This is similar to the CSS property display:none.
Syntax
$(selector).hide(speed,easing,callback)
toggle() method hides elements
Switches between hide() and show() on the selected element.
This method checks the visible status of the selected element. If an element is hidden, show() is run, if an element is visible, hide() is run - this creates a toggle effect.
Note: Hidden elements will not be fully displayed (no longer affect the layout of the page).
Tip: This method can be used to switch between custom functions.
Grammar
$(selector).toggle(speed,easing,callback)
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>132</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $(".btn1").click(function(){ $("#p1").hide(); }); $(".btn2").click(function(){ $("#p1").show(); }); }); </script> </head> <body> <p id="p1">这是一个段落。</p> <button class="btn1">隐藏</button> <button class="btn2">显示</button> </body> </html>
Output result:
Related video tutorial recommendations: jQuery video tutorial
The above is the detailed content of How to hide div based on id in jquery. For more information, please follow other related articles on the PHP Chinese website!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

HTML5aimstoenhancewebcapabilities,makingitmoredynamic,interactive,andaccessible.1)Itsupportsmultimediaelementslikeand,eliminatingtheneedforplugins.2)Semanticelementsimproveaccessibilityandcodereadability.3)Featureslikeenablepowerful,responsivewebappl

HTML5aimstoenhancewebdevelopmentanduserexperiencethroughsemanticstructure,multimediaintegration,andperformanceimprovements.1)Semanticelementslike,,,andimprovereadabilityandaccessibility.2)andtagsallowseamlessmultimediaembeddingwithoutplugins.3)Featur

HTML5isnotinherentlyinsecure,butitsfeaturescanleadtosecurityrisksifmisusedorimproperlyimplemented.1)Usethesandboxattributeiniframestocontrolembeddedcontentandpreventvulnerabilitieslikeclickjacking.2)AvoidstoringsensitivedatainWebStorageduetoitsaccess

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
