With the development and popularization of network technology, more and more website applications have begun to adopt dynamic page technology, among which the JavaScript framework jQuery is the most widely used one. In jQuery, getting elements is one of the most basic operations. However, some node elements may become invisible due to some style settings or position restrictions, and it is these invisible node elements that are often one of the targets we need to operate on in actual development. So how to get invisible elements using jQuery?
1. Type analysis of invisible elements
In jQuery, there are four common invisible elements: display is none, visibility is hidden, element width or height is 0, or itself Off screen (top or left is too large or small).
1. display is none
These elements exist in the HTML page, but they are not displayed on the page and do not occupy the page layout space.
For example, in the code example below in HTML, although there are two div elements, only one div is visible on the page:
<div>这是一个可见的div元素</div> <div>这是一个不可见的div元素</div>
2, visibility is hidden
This part of the element is similar to the display-none type, but the element is invisible and takes up the layout space of the page. For example, in the code example below in HTML, although there are two div elements, both divs occupy the layout space on the page, but one is visible and the other is invisible:
<div>这是一个可见的div元素</div> <div>这是一个不可见但占据布局空间的div元素</div>
3, The element width or height is 0
In this case, the layout occupied by the element will not affect the layout of the page, but because the width or height is 0, we cannot directly find and operate the element.
For example, in the code example below in HTML, although there are three div elements, only two divs are visible on the page, and the other two are invisible. The width and height of the "notVisibleDiv" element are both 0, so it is invisible and cannot be found through ordinary selectors:
<div>这是一个可见的div元素</div> <div>这是一个宽高都为0的不可见div元素</div> <div>这是一个宽高都为0的、本身就在页面之外的不可见div元素</div>
4. It is outside the screen (top or left is too large or too small)
These elements also exist in the page, but usually JavaScript code is required to find and operate them.
For example, in the code example below in HTML, although there are three div elements, only one div is visible on the page, and the other two are invisible. Among them, the left value of the "leftSidedDiv" element is too small and is outside the page, while the left value of the "rightSidedDiv" element is too large, and similar problems occur:
<div>这是一个可见的div元素</div> <div>这是一个left值过小的不可见div元素</div> <div>这是一个left值过大的不可见div元素</div>
2. How to find invisible elements with jQuery
Because there are these invisible elements, during the actual development process, we may need to find them and perform certain operations, such as getting the attribute values of invisible elements, setting new styles for invisible elements, etc.
Now let’s learn how to find invisible elements in jQuery. Based on the above analysis, we can get the following four methods:
1. Selector
You can use the same selector to find invisible elements to find visible elements. Since the search scope is the entire document, this method requires a certain amount of calculation time. But the advantage is that the code is simple and easy to understand.
For example, we want to get the element "leftSidedDiv" located outside the screen:
var notVisibleEle = $('#leftSidedDiv');
2. Filter selector
The filter can filter out elements that meet specific attribute conditions.
For example, we want to get the element whose position attribute value is absolute:
var notVisibleEle = $('div').filter(function() { return $(this).css('position') == 'absolute'; });
3. Hide or show invisible elements
This is equivalent to moving the invisible element to a Within the controllable range, and then obtain the attribute value.
For example, we want to get the element "notVisibleDiv" with a width and height of 0:
var hoverEleWidth = $('#notVisibleDiv').show().width(); var hoverEleHeight = $('#notVisibleDiv').show().height(); $('#notVisibleDiv').hide();
4. Traversing the document tree
jQuery can find the document by traversing Invisible elements in the tree. This approach results in better performance because only part of the document tree needs to be traversed rather than the entire document tree.
For example, we want to get all off-screen elements:
$('#containerDiv').find('*').filter(function() { return $(this).position().left $(document).width(); });
3. Application of invisible elements
After understanding how to get invisible elements, we can actually develop Flexible application in the process. Below are some specific application scenarios.
1. Dynamically modify the attributes of invisible elements
For example, the div element we specified in the web page was originally invisible, but when the mouse hovers over a button, This div needs to be displayed, and when the mouse is moved away, this state needs to be restored.
<div>这是一个鼠标悬停时显示的div元素</div> <button>悬停按钮</button>
// 鼠标悬停时显示隐藏 $('#hoverBtn').hover( function() { $('#hoverDiv').show(); }, function() { $('#hoverDiv').hide(); } );
2. Detect the size of invisible elements
For example, we need to get the size of the target node and cover it in the floating layer
<div>这是一个宽高都为0的不可见元素</div> <div>这是一个浮层容器,将会包裹住目标节点</div>
// 获取目标节点大小 var targetWidth = $('.targetEle').show().width(); var targetHeight= $('.targetEle').show().height(); // 填充浮层中内容并显示 var popupContent = '目标节点的大小为宽:' + targetWidth + 'px,高:' + targetHeight + 'px'; $('.popupBox').html(popupContent).show(); $('.targetEle').hide();
3. Get the invisible element Element attributes and operations
For example, when the mouse hovers over a certain navigation, the image address under the corresponding navigation can be obtained, and then the image can be displayed on the page through an Ajax request.
<div>导航项1</div> <div>导航项2</div> <div>导航项3</div> <div></div>
// 鼠标悬停时操作 $('.navItem').hover( function() { var picUrl = $(this).data('pic-url'); $.ajax({ url: picUrl, success: function(data) { $('.imageContainer').html('<img src="/static/imghwm/default1.png" data-src="' + data + '" class="lazy" alt="jquery method and application for obtaining invisible elements" >'); } }); }, function() { $('.imageContainer').empty(); } );
Summary:
JQuery obtaining invisible elements involves multiple methods such as selectors, filtering selectors, hiding or showing invisible elements, and traversing the document tree. The flexible application of these methods can better meet our needs in actual development.
The above is the detailed content of jquery method and application for obtaining invisible elements. For more information, please follow other related articles on the PHP Chinese website!

HTML and React can be seamlessly integrated through JSX to build an efficient user interface. 1) Embed HTML elements using JSX, 2) Optimize rendering performance using virtual DOM, 3) Manage and render HTML structures through componentization. This integration method is not only intuitive, but also improves application performance.

React efficiently renders data through state and props, and handles user events through the synthesis event system. 1) Use useState to manage state, such as the counter example. 2) Event processing is implemented by adding functions in JSX, such as button clicks. 3) The key attribute is required to render the list, such as the TodoList component. 4) For form processing, useState and e.preventDefault(), such as Form components.

React interacts with the server through HTTP requests to obtain, send, update and delete data. 1) User operation triggers events, 2) Initiate HTTP requests, 3) Process server responses, 4) Update component status and re-render.

React is a JavaScript library for building user interfaces that improves efficiency through component development and virtual DOM. 1. Components and JSX: Use JSX syntax to define components to enhance code intuitiveness and quality. 2. Virtual DOM and Rendering: Optimize rendering performance through virtual DOM and diff algorithms. 3. State management and Hooks: Hooks such as useState and useEffect simplify state management and side effects handling. 4. Example of usage: From basic forms to advanced global state management, use the ContextAPI. 5. Common errors and debugging: Avoid improper state management and component update problems, and use ReactDevTools to debug. 6. Performance optimization and optimality

Reactisafrontendlibrary,focusedonbuildinguserinterfaces.ItmanagesUIstateandupdatesefficientlyusingavirtualDOM,andinteractswithbackendservicesviaAPIsfordatahandling,butdoesnotprocessorstoredataitself.

React can be embedded in HTML to enhance or completely rewrite traditional HTML pages. 1) The basic steps to using React include adding a root div in HTML and rendering the React component via ReactDOM.render(). 2) More advanced applications include using useState to manage state and implement complex UI interactions such as counters and to-do lists. 3) Optimization and best practices include code segmentation, lazy loading and using React.memo and useMemo to improve performance. Through these methods, developers can leverage the power of React to build dynamic and responsive user interfaces.

React is a JavaScript library for building modern front-end applications. 1. It uses componentized and virtual DOM to optimize performance. 2. Components use JSX to define, state and attributes to manage data. 3. Hooks simplify life cycle management. 4. Use ContextAPI to manage global status. 5. Common errors require debugging status updates and life cycles. 6. Optimization techniques include Memoization, code splitting and virtual scrolling.

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.


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

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool