0

以下の例で追加引数をコーディングするクリーンな方法を探しています。

imgBox$.append("<div class='cellGrip cellGrip_se' title='Drag'></div><div class='cellGrip cellGrip_sw' title='Drag'></div><div class='cellGrip cellGrip_ne' title='Drag'></div><div class='cellGrip cellGrip_nw' title='Drag'></div>");

上記は機能しますが、わかりやすくするために、次のように記述します。

imgBox$.append("<div class='cellGrip cellGrip_se' title='Drag'></div>
                <div class='cellGrip cellGrip_sw' title='Drag'></div>
                <div class='cellGrip cellGrip_ne' title='Drag'></div>
                <div class='cellGrip cellGrip_nw' title='Drag'></div>");

しかし、その後、文字列が終了していないという苦情が寄せられます。

アイデアをありがとう。

4

4 に答える 4

2

あなたはこれを行うことができます...

imgBox$.append("<div class='cellGrip cellGrip_se' title='Drag'></div>" +
               "<div class='cellGrip cellGrip_sw' title='Drag'></div>" +
               "<div class='cellGrip cellGrip_ne' title='Drag'></div>" +
               "<div class='cellGrip cellGrip_nw' title='Drag'></div>");

またはこれ...

imgBox$.append("<div class='cellGrip cellGrip_se' title='Drag'></div>\
                <div class='cellGrip cellGrip_sw' title='Drag'></div>\
                <div class='cellGrip cellGrip_ne' title='Drag'></div>\
                <div class='cellGrip cellGrip_nw' title='Drag'></div>");

ただし、2 つ目は各行の先頭にすべてのスペースを挿入します。

于 2013-10-01T22:04:14.327 に答える
1

リテラル改行をエスケープできます

imgBox$.append("<div class='cellGrip cellGrip_se' title='Drag'></div> \
                <div class='cellGrip cellGrip_sw' title='Drag'></div> \
                <div class='cellGrip cellGrip_ne' title='Drag'></div> \
                <div class='cellGrip cellGrip_nw' title='Drag'></div>");

個人的には、むしろやりたい:

var arr  = ['se', 'sw', 'ne', 'nw'],
    frag = document.createDocumentFragment();

$.each(arr, function(_, key) {
    var div = $('<div />', {'class':'cellGrip cellGrip_'+key, title: 'Drag'});
    $(frag).append(div);
});

imgBox$.append(frag);
于 2013-10-01T22:04:46.457 に答える
1

を追加するだけ\です。

imgBox$.append("<div class='cellGrip cellGrip_se' title='Drag'>a</div> \
                <div class='cellGrip cellGrip_sw' title='Drag'>b</div> \
                <div class='cellGrip cellGrip_ne' title='Drag'>c</div> \
                <div class='cellGrip cellGrip_nw' title='Drag'>d</div>");
于 2013-10-01T22:04:55.043 に答える
1

繰り返して文字列を作成し、追加することができます

var dir = ["se","sw","ne","nw"];
var divs = "";
for( var i = 0; i < 4; i++ ){
 divs += "<div class='cellGrip cellGrip_"+dir[i]+"' title='Drag'></div>";
}
imgBox$.append(divs);
于 2013-10-01T22:07:05.410 に答える