jQuery Plugin for Cloning Element Styles to a Pseudo Clone
Creating a pseudo clone of an element with different tags can be achieved using various approaches in jQuery. Here's a detailed explanation and solutions:
jQuery's getComputedStyle Plugin
For the specific requirement of returning an object of computed styles for an element, you can utilize the jQuery getStyleObject plugin, which retrieves all possible styles from any element, including IE browsers.
Usage:
var style = $("#original").getStyleObject();
Modifying jQuery's CSS Method
Another approach is to modify jQuery's css method as follows:
jQuery.fn.css2 = jQuery.fn.css; jQuery.fn.css = function() { if (arguments.length) return jQuery.fn.css2.apply(this, arguments); var styleObj = {}; // List of style properties to get var styleList = ['font-family','font-size','font-weight','font-style','color', 'text-transform','text-decoration','letter-spacing','word-spacing', 'line-height','text-align','vertical-align','direction','background-color', 'background-image','background-repeat','background-position', 'background-attachment','opacity','width','height','top','right','bottom', 'left','margin-top','margin-right','margin-bottom','margin-left', 'padding-top','padding-right','padding-bottom','padding-left', 'border-top-width','border-right-width','border-bottom-width', 'border-left-width','border-top-color','border-right-color', 'border-bottom-color','border-left-color','border-top-style', 'border-right-style','border-bottom-style','border-left-style','position', 'display','visibility','z-index','overflow-x','overflow-y','white-space', 'clip','float','clear','cursor','list-style-image','list-style-position', 'list-style-type','marker-offset']; for (var i = 0; i < styleList.length; i++) { styleObj[styleList[i]] = jQuery.fn.css2.call(this, styleList[i]); } return styleObj; };
With this modification, you can get the computed style object for the first matched element by calling:
var style = $('#original').css();
Other Editing Approaches
For the general problem of pseudo cloning an element for inline editing, consider these alternative approaches:
The above is the detailed content of How to Pseudo Clone Element Styles Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!