`):** ```javascript $("p:visible") ``` * **Select visible child elements within a specific container:** ```javascript $("#container-id .item:visible") ``` --- ## Code Examples ### Example 1: Basic Usage The following example demonstrates how to select and style all visible `
` elements when a button is clicked. ```html
jQuery :visible Selector Demo
This is a visible paragraph.
This is another visible paragraph.
``` ### Example 2: Toggling and Counting Visible Elements You can combine `:visible` with other jQuery methods to perform dynamic checks, such as counting how many elements are currently displayed on the screen. ```javascript // Count the number of visible list items var visibleItemsCount = $("li:visible").length; console.log("There are " + visibleItemsCount + " visible items."); ``` --- ## Performance Considerations Because `:visible` is a jQuery extension and not part of the official CSS specification, queries using `:visible` cannot take advantage of the high-speed native DOM `querySelectorAll()` method. To achieve optimal performance when using this selector: 1. **Narrow down the search space first:** Avoid using `$(":visible")` globally. Instead, prefix it with a tag name, ID, or class name (e.g., `$("#menu-items .item:visible")`). 2. **Filter an existing jQuery collection:** ```javascript // Recommended for better performance on large DOM trees: $("p").filter(":visible").css("color", "red"); ```
YouTip