You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jQuery对多个指定前缀ID元素执行操作但排除特定ID?

How to Exclude #ids4 from jQuery's ID Prefix Selection

Absolutely, you can definitely exclude #ids4 while targeting all elements with IDs starting with "ids". Here are two straightforward, reliable ways to achieve this:

Method 1: Combine the Attribute Selector with :not()

Use the :not() pseudo-selector directly in your jQuery query to filter out the specific ID:

$('[id^="ids"]:not(#ids4)').html('hello');

This tells jQuery to select every element whose ID starts with "ids" except the element with the exact ID ids4. It’s a concise one-liner for simple exclusions.

Method 2: Use the .not() Filter Method

Alternatively, you can first select all matching elements, then filter out the unwanted one using jQuery’s .not() method:

$('[id^="ids"]').not('#ids4').html('hello');

This approach shines if you ever need to exclude multiple elements later—just pass a comma-separated list of IDs to .not(), like .not('#ids4, #ids6'), for more complex filtering.

Both methods will update the HTML of #ids1, #ids2, #ids3, #ids5, and any future elements with IDs starting with "ids"—while leaving #ids4 completely untouched.

内容的提问来源于stack exchange,提问作者shaikh Fahim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:48:12