1

私は次のようなアンカーのセットを持っています

<a class="lb" href="#">text</a>
<a class="lb" href="#" style="width:200px">text</a>
<a class="lb" href="#" style="color: reen; width:200px">text</a>

これは、次のように変換する必要があります。

<a class="lb" href="#"><span>text</span></a>
<a class="lb" href="#"><span style="width:200px">text</span></a>
<a class="lb" href="#" style="color:green"><span style="width:200px">text</span></a>

子の作成に問題はありませんが、親のスタイリングspanを移動する方法がわかりません。width

4

2 に答える 2

4

あなたの例では、.wrapInner()および.css()関数がそれを行う必要があります。例:

$(document).ready(function(){
   var $anchors = $('a');
   $anchors.wrapInner('<span></span>');

   $anchors.children('span').each(function(i,v){
      $(this).css('width', $(this).parent().css('width'));
   });
});

説明: 一致した要素のセット内の各要素のコンテンツを HTML 構造で囲みます。

このコード例では、マークアップ内のすべてのアンカーが一致することに注意してください。classesまたはを使用して、より具体的にしたい場合があります。ids

于 2010-06-14T07:08:42.327 に答える
0

これでうまくいくはずです-内部を設定し、 :<span>で指定されているcss属性をコピーします。to_be_moved

$('.lb').each(function(){
    $(this).wrapInner('<span/>');
    var to_be_moved = ['width']; // array of css attributes to move
    for (var i=0;i<to_be_moved.length;i++){
        // get current value
        var currentValue = $(this).css(to_be_moved[i]);
        // set current value to nothing
        $(this).css(to_be_moved[i], '');
        // place the value into the child span
        $(this).children('span').css(to_be_moved[i], currentValue);
    }
});
于 2010-06-14T07:31:46.700 に答える