npx skills add https://github.com/jezweb/claude-skills --skill jquery-4状态:生产就绪 最后更新:2026-01-25 依赖项:无 最新版本:jquery@4.0.0, jquery-migrate@4.0.2
升级前,添加 migrate 插件以识别兼容性问题:
<!-- 开发环境:显示已弃用功能的控制台警告 -->
<script src="https://code.jquery.com/jquery-4.0.0.js"></script>
<script src="https://code.jquery.com/jquery-migrate-4.0.2.js"></script>
为什么这很重要:
npm install jquery@4.0.0
# 或安装 migrate 插件用于测试
npm install jquery@4.0.0 jquery-migrate@4.0.2
运行您的应用程序并检查控制台中的 migrate 插件警告。每个警告都表示需要更新的代码。
这些函数已被弃用,现已被移除。请使用原生 JavaScript 等效方法:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 已移除 | 原生替代方案 |
|---|---|
$.isArray(arr) | Array.isArray(arr) |
$.parseJSON(str) | JSON.parse(str) |
$.trim(str) | str.trim() 或 String.prototype.trim.call(str) |
$.now() | Date.now() |
$.type(obj) | typeof obj + Array.isArray() + instanceof |
$.isNumeric(val) | !isNaN(parseFloat(val)) && isFinite(val) |
$.isFunction(fn) | typeof fn === 'function' |
$.isWindow(obj) | obj != null && obj === obj.window |
$.camelCase(str) | 自定义函数(见下文) |
$.nodeName(el, name) | el.nodeName.toLowerCase() === name.toLowerCase() |
camelCase 替代方案:
// $.camelCase 的原生替代方案
function camelCase(str) {
return str.replace(/-([a-z])/g, (match, letter) => letter.toUpperCase());
}
从 jQuery 对象中移除了三个内部数组方法:
// 旧方法 - 在 jQuery 4.0 中不再有效
$elems.push(elem);
$elems.sort(compareFn);
$elems.splice(index, count);
// 新方法 - 使用 call/apply 调用数组方法
[].push.call($elems, elem);
[].sort.call($elems, compareFn);
[].splice.call($elems, index, count);
// 或先转换为数组
const arr = $.makeArray($elems);
arr.push(elem);
jQuery 4.0 遵循 W3C 规范的焦点事件顺序:
// jQuery 3.x 顺序(非标准):
// focusout → blur → focusin → focus
// jQuery 4.0 顺序(W3C 标准):
// blur → focusout → focus → focusin
影响:如果您的代码依赖特定的事件顺序,请彻底测试。
// 示例:可能需要调整的代码
$input.on('blur focusout focus focusin', function(e) {
console.log(e.type); // 4.0 中的顺序已改变
});
精简版构建(jquery-4.0.0.slim.min.js)不再包含:
// 如果使用精简版,请用 Promises 替换 Deferreds
// 旧方法 - Deferred
const deferred = $.Deferred();
deferred.resolve(value);
deferred.promise();
// 新方法 - 原生 Promise
const promise = new Promise((resolve, reject) => {
resolve(value);
});
移除了 toggleClass(boolean) 和 toggleClass(undefined) 签名:
// 旧方法 - 不再有效
$elem.toggleClass(true); // 添加所有类
$elem.toggleClass(false); // 移除所有类
// 新方法 - 明确指定
$elem.addClass('class1 class2'); // 添加类
$elem.removeClass('class1 class2'); // 移除类
// 或使用带类名的 toggleClass
$elem.toggleClass('active', true); // 强制添加
$elem.toggleClass('active', false); // 强制移除
通过 AJAX 获取的脚本不再自动执行,除非指定了 dataType:
// 旧方法 - 脚本自动执行
$.get('script.js');
// 新方法 - 必须指定 dataType 才能自动执行
$.get({
url: 'script.js',
dataType: 'script'
});
// 或使用 $.getScript(仍然有效)
$.getScript('script.js');
| 已移除 | 说明 |
|---|---|
$.cssNumber | 已移除 - 如果需要请本地定义 |
$.cssProps | 不再需要 - 供应商前缀已过时 |
$.fx.interval | 已移除 - requestAnimationFrame 处理此功能 |
# 检查 WordPress 中的当前 jQuery 版本
wp eval "echo wp_scripts()->registered['jquery-core']->ver;"
WordPress 主题/插件应该:
// 取消注册旧版 jQuery 并注册 4.0 版本(仅用于测试)
function upgrade_jquery_for_testing() {
if (!is_admin()) {
wp_deregister_script('jquery-core');
wp_deregister_script('jquery');
wp_register_script('jquery-core',
'https://code.jquery.com/jquery-4.0.0.min.js',
array(), '4.0.0', true);
wp_register_script('jquery', false, array('jquery-core'), '4.0.0', true);
// 添加 migrate 插件用于调试
wp_enqueue_script('jquery-migrate',
'https://code.jquery.com/jquery-migrate-4.0.2.min.js',
array('jquery'), '4.0.2', true);
}
}
add_action('wp_enqueue_scripts', 'upgrade_jquery_for_testing', 1);
许多 WordPress 插件使用已移除的 jQuery 方法:
// 旧插件中的常见模式 - 在 4.0 中已失效
if ($.isArray(data)) { ... }
var json = $.parseJSON(response);
var cleaned = $.trim(userInput);
// 修复:更新为原生方法
if (Array.isArray(data)) { ... }
var json = JSON.parse(response);
var cleaned = userInput.trim();
// 旧版 jQuery 类型检查
if ($.type(value) === 'array') { ... }
if ($.type(value) === 'function') { ... }
if ($.type(value) === 'object') { ... }
if ($.type(value) === 'string') { ... }
if ($.type(value) === 'number') { ... }
// 新版原生类型检查
if (Array.isArray(value)) { ... }
if (typeof value === 'function') { ... }
if (value !== null && typeof value === 'object' && !Array.isArray(value)) { ... }
if (typeof value === 'string') { ... }
if (typeof value === 'number') { ... }
如果您需要快速兼容性而不更改所有代码:
// 为已移除的方法提供垫片(临时迁移辅助)
if (typeof $.isArray === 'undefined') {
$.isArray = Array.isArray;
}
if (typeof $.parseJSON === 'undefined') {
$.parseJSON = JSON.parse;
}
if (typeof $.trim === 'undefined') {
$.trim = function(str) {
return str == null ? '' : String.prototype.trim.call(str);
};
}
if (typeof $.now === 'undefined') {
$.now = Date.now;
}
if (typeof $.isFunction === 'undefined') {
$.isFunction = function(fn) {
return typeof fn === 'function';
};
}
if (typeof $.isNumeric === 'undefined') {
$.isNumeric = function(val) {
return !isNaN(parseFloat(val)) && isFinite(val);
};
}
重要:这只是临时措施。请更新您的代码以使用原生方法。
jQuery 4.0 支持 ES 模块:
// ES 模块导入(4.0 新增)
import $ from 'jquery';
// 或使用命名导出
import { $ } from 'jquery';
// 在 package.json 中,确保模块解析
{
"type": "module"
}
对于带有可信类型的 CSP:
// jQuery 4.0 在 DOM 操作中接受 TrustedHTML
import DOMPurify from 'dompurify';
// 创建可信 HTML
const clean = DOMPurify.sanitize(untrustedHTML, {RETURN_TRUSTED_TYPE: true});
// 与 jQuery 4.0 一起使用是安全的
$('#container').html(clean);
dataType: 'script'$.type() - 使用原生类型检查toggleClass(boolean) 签名此技能预防 8 个已记录的问题:
错误:TypeError: $.isArray is not a function 来源:https://github.com/jquery/jquery/issues/5411 原因:方法在 jQuery 4.0 中已移除 预防:改用 Array.isArray()
错误:TypeError: $.parseJSON is not a function 来源:https://jquery.com/upgrade-guide/4.0/ 原因:自 3.0 起已弃用,4.0 中移除 预防:改用 JSON.parse()
错误:TypeError: $.trim is not a function 来源:https://jquery.com/upgrade-guide/4.0/ 原因:原生 String.prototype.trim 随处可用 预防:使用 str.trim() 或 String.prototype.trim.call(str)
错误:表单验证中出现意外行为 来源:https://blog.jquery.com/2026/01/17/jquery-4-0-0/ 原因:jQuery 4.0 遵循 W3C 规范,而非旧版顺序 预防:测试并更新依赖顺序的事件处理程序
错误:TypeError: $.Deferred is not a function 来源:https://blog.jquery.com/2026/01/17/jquery-4-0-0/ 原因:4.0 中已从精简版移除 预防:使用完整版或原生 Promises
错误:脚本已加载但未执行 来源:https://jquery.com/upgrade-guide/4.0/ 原因:未明确指定 dataType 时禁用自动执行 预防:在 AJAX 选项中添加 dataType: 'script'
错误:toggleClass(true) 无效 来源:https://jquery.com/upgrade-guide/4.0/ 原因:布尔签名已移除 预防:使用 addClass/removeClass 或带类名的 toggleClass
错误:各种 "is not a function" 错误 来源:WordPress 生态系统中常见 原因:插件使用已移除的 jQuery 方法 预防:升级前使用 jquery-migrate 审计插件
| 功能 | 完整版 | 精简版 |
|---|---|---|
| 大小(gzip 压缩) | ~27.5k | ~19.5k |
| DOM 操作 | 是 | 是 |
| 事件 | 是 | 是 |
| AJAX | 是 | 否 |
| 效果/动画 | 是 | 否 |
| Deferreds | 是 | 否 |
| Callbacks | 是 | 否 |
使用精简版当:静态网站,无需 AJAX,使用原生 fetch/Promises
使用完整版当:WordPress,AJAX 密集型应用,需要 $.animate 或 Deferreds
{
"dependencies": {
"jquery": "^4.0.0"
},
"devDependencies": {
"jquery-migrate": "^4.0.2"
}
}
解决方案:仅升级前端 jQuery。管理后台使用自己的版本。使用条件逻辑避免影响 wp-admin。
解决方案:保持 jquery-migrate 加载直到插件更新。检查插件更新日志以获取 jQuery 4.0 兼容性更新。
解决方案:在 $.ajax 选项中添加 dataType: 'script' 或使用 $.getScript() 加载脚本。
解决方案:检查 focus/blur/focusin/focusout 处理程序。jQuery 4.0 触发顺序:blur → focusout → focus → focusin(W3C 顺序)。
有问题?遇到问题?
每周安装次数
232
代码仓库
GitHub 星标数
656
首次出现
2026年1月27日
安全审计
安装于
claude-code190
opencode154
gemini-cli153
cursor142
codex138
antigravity129
Status : Production Ready Last Updated : 2026-01-25 Dependencies : None Latest Versions : jquery@4.0.0, jquery-migrate@4.0.2
Before upgrading, add the migrate plugin to identify compatibility issues:
<!-- Development: Shows console warnings for deprecated features -->
<script src="https://code.jquery.com/jquery-4.0.0.js"></script>
<script src="https://code.jquery.com/jquery-migrate-4.0.2.js"></script>
Why this matters:
npm install jquery@4.0.0
# Or with migrate plugin for testing
npm install jquery@4.0.0 jquery-migrate@4.0.2
Run your application and check console for migrate plugin warnings. Each warning indicates code that needs updating.
These functions were deprecated and are now removed. Use native JavaScript equivalents:
| Removed | Native Replacement |
|---|---|
$.isArray(arr) | Array.isArray(arr) |
$.parseJSON(str) | JSON.parse(str) |
$.trim(str) | str.trim() or String.prototype.trim.call(str) |
$.now() |
camelCase replacement:
// Native replacement for $.camelCase
function camelCase(str) {
return str.replace(/-([a-z])/g, (match, letter) => letter.toUpperCase());
}
Three internal array methods removed from jQuery objects:
// OLD - No longer works in jQuery 4.0
$elems.push(elem);
$elems.sort(compareFn);
$elems.splice(index, count);
// NEW - Use array methods with call/apply
[].push.call($elems, elem);
[].sort.call($elems, compareFn);
[].splice.call($elems, index, count);
// Or convert to array first
const arr = $.makeArray($elems);
arr.push(elem);
jQuery 4.0 follows the W3C specification for focus event order:
// jQuery 3.x order (non-standard):
// focusout → blur → focusin → focus
// jQuery 4.0 order (W3C standard):
// blur → focusout → focus → focusin
Impact : If your code depends on specific event ordering, test thoroughly.
// Example: code that may need adjustment
$input.on('blur focusout focus focusin', function(e) {
console.log(e.type); // Order changed in 4.0
});
The slim build (jquery-4.0.0.slim.min.js) no longer includes:
Deferreds and Callbacks (use native Promises instead)
AJAX functionality
Animation effects
// If using slim build, replace Deferreds with Promises // OLD - Deferred const deferred = $.Deferred(); deferred.resolve(value); deferred.promise();
// NEW - Native Promise const promise = new Promise((resolve, reject) => { resolve(value); });
The toggleClass(boolean) and toggleClass(undefined) signatures are removed:
// OLD - No longer works
$elem.toggleClass(true); // Added all classes
$elem.toggleClass(false); // Removed all classes
// NEW - Be explicit
$elem.addClass('class1 class2'); // Add classes
$elem.removeClass('class1 class2'); // Remove classes
// Or use toggleClass with class names
$elem.toggleClass('active', true); // Force add
$elem.toggleClass('active', false); // Force remove
Scripts fetched via AJAX no longer auto-execute unless dataType is specified:
// OLD - Scripts auto-executed
$.get('script.js');
// NEW - Must specify dataType for auto-execution
$.get({
url: 'script.js',
dataType: 'script'
});
// Or use $.getScript (still works)
$.getScript('script.js');
| Removed | Notes |
|---|---|
$.cssNumber | Removed - define locally if needed |
$.cssProps | No longer needed - vendor prefixes obsolete |
$.fx.interval | Removed - requestAnimationFrame handles this |
# Check current jQuery version in WordPress
wp eval "echo wp_scripts()->registered['jquery-core']->ver;"
WordPress themes/plugins should:
// Dequeue old jQuery and enqueue 4.0 (testing only)
function upgrade_jquery_for_testing() {
if (!is_admin()) {
wp_deregister_script('jquery-core');
wp_deregister_script('jquery');
wp_register_script('jquery-core',
'https://code.jquery.com/jquery-4.0.0.min.js',
array(), '4.0.0', true);
wp_register_script('jquery', false, array('jquery-core'), '4.0.0', true);
// Add migrate plugin for debugging
wp_enqueue_script('jquery-migrate',
'https://code.jquery.com/jquery-migrate-4.0.2.min.js',
array('jquery'), '4.0.2', true);
}
}
add_action('wp_enqueue_scripts', 'upgrade_jquery_for_testing', 1);
Many WordPress plugins use removed jQuery methods:
// Common pattern in older plugins - BROKEN in 4.0
if ($.isArray(data)) { ... }
var json = $.parseJSON(response);
var cleaned = $.trim(userInput);
// Fix: Update to native methods
if (Array.isArray(data)) { ... }
var json = JSON.parse(response);
var cleaned = userInput.trim();
// OLD jQuery type checking
if ($.type(value) === 'array') { ... }
if ($.type(value) === 'function') { ... }
if ($.type(value) === 'object') { ... }
if ($.type(value) === 'string') { ... }
if ($.type(value) === 'number') { ... }
// NEW Native type checking
if (Array.isArray(value)) { ... }
if (typeof value === 'function') { ... }
if (value !== null && typeof value === 'object' && !Array.isArray(value)) { ... }
if (typeof value === 'string') { ... }
if (typeof value === 'number') { ... }
If you need quick compatibility without changing all code:
// Polyfill removed methods (temporary migration aid)
if (typeof $.isArray === 'undefined') {
$.isArray = Array.isArray;
}
if (typeof $.parseJSON === 'undefined') {
$.parseJSON = JSON.parse;
}
if (typeof $.trim === 'undefined') {
$.trim = function(str) {
return str == null ? '' : String.prototype.trim.call(str);
};
}
if (typeof $.now === 'undefined') {
$.now = Date.now;
}
if (typeof $.isFunction === 'undefined') {
$.isFunction = function(fn) {
return typeof fn === 'function';
};
}
if (typeof $.isNumeric === 'undefined') {
$.isNumeric = function(val) {
return !isNaN(parseFloat(val)) && isFinite(val);
};
}
CRITICAL : This is a temporary measure. Update your code to use native methods.
jQuery 4.0 supports ES modules:
// ES Module import (new in 4.0)
import $ from 'jquery';
// Or with named export
import { $ } from 'jquery';
// In package.json, ensure module resolution
{
"type": "module"
}
For CSP with Trusted Types:
// jQuery 4.0 accepts TrustedHTML in DOM manipulation
import DOMPurify from 'dompurify';
// Create trusted HTML
const clean = DOMPurify.sanitize(untrustedHTML, {RETURN_TRUSTED_TYPE: true});
// Safe to use with jQuery 4.0
$('#container').html(clean);
dataType: 'script' for AJAX script loading$.type() - use native type checkingtoggleClass(boolean) signatureThis skill prevents 8 documented issues:
Error : TypeError: $.isArray is not a function Source : https://github.com/jquery/jquery/issues/5411 Why It Happens : Method removed in jQuery 4.0 Prevention : Use Array.isArray() instead
Error : TypeError: $.parseJSON is not a function Source : https://jquery.com/upgrade-guide/4.0/ Why It Happens : Deprecated since 3.0, removed in 4.0 Prevention : Use JSON.parse() instead
Error : TypeError: $.trim is not a function Source : https://jquery.com/upgrade-guide/4.0/ Why It Happens : Native String.prototype.trim available everywhere Prevention : Use str.trim() or String.prototype.trim.call(str)
Error : Unexpected behavior in form validation Source : https://blog.jquery.com/2026/01/17/jquery-4-0-0/ Why It Happens : jQuery 4.0 follows W3C spec, not legacy order Prevention : Test and update event handlers that depend on order
Error : TypeError: $.Deferred is not a function Source : https://blog.jquery.com/2026/01/17/jquery-4-0-0/ Why It Happens : Removed from slim build in 4.0 Prevention : Use full build or native Promises
Error : Script loaded but not executed Source : https://jquery.com/upgrade-guide/4.0/ Why It Happens : Auto-execution disabled without explicit dataType Prevention : Add dataType: 'script' to AJAX options
Error : toggleClass(true) has no effect Source : https://jquery.com/upgrade-guide/4.0/ Why It Happens : Boolean signature removed Prevention : Use addClass/removeClass or toggleClass with class names
Error : Various "is not a function" errors Source : Common in WordPress ecosystem Why It Happens : Plugins using removed jQuery methods Prevention : Audit plugins with jquery-migrate before upgrading
| Feature | Full Build | Slim Build |
|---|---|---|
| Size (gzipped) | ~27.5k | ~19.5k |
| DOM Manipulation | Yes | Yes |
| Events | Yes | Yes |
| AJAX | Yes | No |
| Effects/Animation | Yes | No |
| Deferreds | Yes | No |
| Callbacks | Yes | No |
Use slim build when : Static sites, no AJAX needs, using native fetch/Promises
Use full build when : WordPress, AJAX-heavy apps, need $.animate or Deferreds
{
"dependencies": {
"jquery": "^4.0.0"
},
"devDependencies": {
"jquery-migrate": "^4.0.2"
}
}
Solution : Only upgrade frontend jQuery. Admin uses its own version. Use conditional logic to avoid affecting wp-admin.
Solution : Keep jquery-migrate loaded until plugins are updated. Check plugin changelogs for jQuery 4.0 compatibility updates.
Solution : Add dataType: 'script' to $.ajax options or use $.getScript() for script loading.
Solution : Review focus/blur/focusin/focusout handlers. jQuery 4.0 fires: blur → focusout → focus → focusin (W3C order).
Questions? Issues?
Weekly Installs
232
Repository
GitHub Stars
656
First Seen
Jan 27, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code190
opencode154
gemini-cli153
cursor142
codex138
antigravity129
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
109,600 周安装
Date.now() |
$.type(obj) | typeof obj + Array.isArray() + instanceof |
$.isNumeric(val) | !isNaN(parseFloat(val)) && isFinite(val) |
$.isFunction(fn) | typeof fn === 'function' |
$.isWindow(obj) | obj != null && obj === obj.window |
$.camelCase(str) | Custom function (see below) |
$.nodeName(el, name) | el.nodeName.toLowerCase() === name.toLowerCase() |