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

如何使用 jQuery 偽克隆元素樣式?

Linda Hamilton
發布: 2024-10-22 13:47:02
原創
392 人瀏覽過

How to Pseudo Clone Element Styles Using jQuery?

用於將元素樣式複製為偽克隆的jQuery 外掛程式

可以使用多種方法來創建具有不同標籤的元素的偽克隆在jQuery 中。這裡有詳細的解釋和解決方案:

jQuery 的getCompulatedStyle 插件

對於傳回元素的計算樣式物件的特定要求,您可以使用jQuery getStyleObject 插件,它從任何元素檢索所有可能的樣式,包括IE 瀏覽器。

用法:

var style = $("#original").getStyleObject();
登入後複製

修改jQuery 的CSS 方法

另一種方法是修改jQuery 的css 方法如下:

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;
};
登入後複製
另一種方法是修改jQuery 的css 方法如下:

var style = $('#original').css();
登入後複製
透過此修改,您可以透過呼叫:

其他編輯來取得第一個符合元素的計算樣式物件方法

    對於偽克隆元素進行內聯編輯的一般問題,請考慮以下替代方法:
  • jQuery.clone() 和.replaceWith() :
  • 克隆原始元素,將其修改為輸入字段,並用克隆替換原始元素。
  • jQuery.replaceWith() 與 .data():
  • 取代帶有輸入欄位的原始元素,將前一個元素的資料儲存在資料屬性中。編輯後,用資料替換輸入。
  • ContentEditable 屬性:
切換元素的 contentEditable 屬性以允許直接內嵌編輯。

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

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