首頁 > web前端 > js教程 > 主體

jQuery 如何使用偽克隆和計算樣式來克隆元素樣式?

DDD
發布: 2024-10-22 11:52:02
原創
758 人瀏覽過

How Can jQuery Clone Element Styles using Pseudo Cloning and Computed Styles?

用於偽克隆元素樣式的jQuery CSS 外掛程式

簡介

使用jQuery 時,有時需要存取和操作計算的樣式一個元素的。雖然現有的 jQuery 方法(如 width())允許輕鬆操作特定屬性,但需要更全面的方法來複製和應用所有計算樣式。

問題陳述

核心挑戰是圍繞尋找一個jQuery 外掛程式或方法,它可以:

  • 傳回一個包含第一個符合元素的計算樣式的對象
  • 允許使用css( ) 方法
  • 促進元素的偽克隆,其中使用不同的標籤但保持相同的外觀

解決方案

儘管是明顯的需求,但現成的用於此任務的jQuery 插件尚未開發。但是,有一些自訂解決方案可以有效解決此問題。

其中一個解決方案涉及透過覆蓋標準 css() 方法的行為來擴展其行為。如果未提供參數,它將傳回一個具有所有計算樣式的物件。

擴充 CSS 方法:

jQuery.fn.css2 = jQuery.fn.css;
jQuery.fn.css = function() {
    if (arguments.length) return jQuery.fn.css2.apply(this, arguments);
    var attr = ['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'];
    var len = attr.length, obj = {};
    for (var i = 0; i < len; i++) 
        obj[attr[i]] = jQuery.fn.css2.call(this, attr[i]);
    return obj;
}
登入後複製

另一個值得注意的解決方案是名為 getStyleObject 的 jQuery 外掛。它會擷取所有可能的樣式,包括那些無法透過標準css() 方法存取的樣式:

getStyleObject 外掛程式:

(function($){
    $.fn.getStyleObject = function(){
        var dom = this.get(0);
        var style;
        var returns = {};
        if(window.getComputedStyle){
            var camelize = function(a,b){
                return b.toUpperCase();
            }
            style = window.getComputedStyle(dom, null);
            for(var i=0;i<style.length;i++){
                var prop = style[i];
                var camel = prop.replace(/\-([a-z])/g, camelize);
                var val = style.getPropertyValue(prop);
                returns[camel] = val;
            }
            return returns;
        }
        if(dom.currentStyle){
            style = dom.currentStyle;
            for(var prop in style){
                returns[prop] = style[prop];
            }
            return returns;
        }
        return this.css();
    }
})(jQuery);
登入後複製

結論

結論結論結論結論結論結論結論結論兩者這些解決方案使您能夠獲取計算樣式並將其應用於元素,從而提供更大的靈活性以及對元素外觀和偽克隆的控制。解決方案的選擇取決於相容性要求和您專案的特定需求。

以上是jQuery 如何使用偽克隆和計算樣式來克隆元素樣式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!