本文档介绍了在 WordPress 主题开发中使用 JavaScript 的最佳实践,旨在确保代码高效运行、避免与内容或插件冲突,并涵盖库使用、编码标准和 jQuery 优化等方面。
// 使用 IIFE 包装器映射 $ 到 jQuery
( function( $ ) {
// 您的代码放在这里
} )( jQuery );
// 缓存 jQuery 选择器以提高性能
var $postImage = $('.post img');
$postImage.addClass('theme-image');
$postImage.on('click', function() { ... });
// 使用事件委托减少事件处理程序数量
$(document).on('click', 'a', logClick);Many themes use JavaScript to provide interactivity, animation or other enhancements. These best practices will help ensure your code works efficiently and does not cause conflicts with your content or plugins.
There are many useful JavaScript libraries you may want to include when building your theme. Did you know that WordPress comes bundled with dozens of popular libraries? Check out this list of default scripts included with WordPress.
A common mistake made by beginning theme and plugin developers is to bundle their theme or plugin with their own version of the library. This may conflict with other plugins that enqueue the version bundled with WordPress. As a best practice, make your theme compatible with the version of your favorite library included with WordPress.
If you feel you MUST replace the WordPress version with one of your own… well… the answer is still: don’t do it. The versions of JavaScript libraries used by WordPress may include custom tweaks that WordPress needs to operate. Any time you override these libraries, you risk breaking your WordPress instance. Moreover, plugins created by other authors should be written to be compatible with WordPress’s version of these libraries as well. Adding your own version may (and often does!) conflict with plugins.
Javascript is growing in popularity for web developers, and it’s being used to accomplish for a variety of tasks. Here are some best practices to use when writing your JavaScript
jQuery is a popular JavaScript library to make working with JavaScript easier and more reliable across browsers. If you use jQuery, be sure to follow the handbook guidelines on including JavaScript. Giving your theme’s enqueued .js files a dependency array of array( 'jquery' ) ensures that the jQuery script has been downloaded and loaded before your theme’s code.
Because the copy of jQuery included in WordPress loads in <a href="https://api.jquery.com/jQuery.noConflict/">noConflict()</a> mode, use this wrapper code in your theme’s .js files to map “$” to “jQuery”:
( function( $ ) {
// Your code goes here
} )( jQuery );
This wrapper (called an Immediately Invoked Function Expression, or IIFE) lets you pass in a variable—jQuery—on the bottom line, and give it the name “$” internally. Within this wrapper you may use $ to select elements as normal.
Every time you select an element with jQuery, it performs a query through your document’s markup. These queries are very fast, but they do take time—you can make your site respond faster by re-using your jQuery objects instead of using a new query. So instead of repeating selectors:
// Anti-pattern
$('.post img').addClass('theme-image');
$('.post img').on('click', function() { ... });
it is recommended to cache your selectors so you can re-use the returned element without having to repeat the lookup process:
var $postImage = $('.post img');
// All image tags within posts can now be accessed through $postImage
$postImage.addClass('theme-image');
$postImage.on('click', function() { ... });
When you use jQuery methods like .bind or .click, jQuery creates a new browser event object to handle processing the requested event. Each new event created takes a small amount of memory, but the amount of memory required goes up the more events you bind. If you have a page with a hundred anchor tags and wanted to fire a `logClick` event handler whenever a user clicked a link, it is very easy to write code like this:
// Anti-pattern
$('a').click( logClick );
This works, but under the hood you have asked jQuery to create a new event handler for every link on your page.
Event delegation is a way to accomplish the same effect with less overhead. Because events in jQuery “bubble”—that is, a click event will fire on a link, then on the link’s surrounding <p> tag, then on the div container, and so on up to the document object itself—we can put a single event handler higher up in the page structure, and still catch the click events for all of those links:
// Bind one handler at the document level, which is triggered
// whenever there is a "click" event originating from an "a" tag
$(document).on('click', 'a', logClick);