ホームページ  >  記事  >  ウェブフロントエンド  >  jQueryを使用して要素スタイルを疑似クローンする方法?

jQueryを使用して要素スタイルを疑似クローンする方法?

Linda Hamilton
Linda Hamiltonオリジナル
2024-10-22 13:47:02393ブラウズ

How to Pseudo Clone Element Styles Using jQuery?

要素スタイルを擬似クローンに複製するための jQuery プラグイン

異なるタグを持つ要素の擬似クローンの作成は、さまざまなアプローチを使用して実現できます。 jQueryで。詳細な説明と解決策は次のとおりです。

jQuery の getComputedStyle プラグイン

要素の計算されたスタイルのオブジェクトを返すという特定の要件については、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;
};

この変更により、次の呼び出しによって最初に一致した要素の計算されたスタイル オブジェクトを取得できます:

var style = $('#original').css();

その他の編集アプローチ

インライン編集のために要素を疑似クローン作成するという一般的な問題については、次の代替アプローチを検討してください:

  • jQuery.clone() と .replaceWith() : 元の要素を複製し、入力フィールドになるように変更し、元の要素をクローンで置き換えます。
  • jQuery.replaceWith() with .data():入力フィールドを持つ元の要素。前の要素のデータをデータ属性に保存します。編集後、入力をデータに置き換えます。
  • ContentEditable 属性: 要素の contentEditable 属性を切り替えて、直接インライン編集できるようにします。

以上がjQueryを使用して要素スタイルを疑似クローンする方法?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。