特定のデータ属性を持つページのすべての要素をドロップダウンに置き換えるために、このコードを作成しました。私が持っていたとしましょう:
<span data-what="partyBox"></span>
ドロップダウンに置き換えられます。コードはうまく機能しますが、例外があります。後で、現在のタグのすべての属性 (たとえば、すべてのデータ属性またはその他の割り当てられた属性) を割り当てたいと思いました。つまり、span
この場合は、作成したドロップダウンに割り当てられるタグです。しかし、これを達成するのに問題があります。つまり、これらすべての属性がドロップダウンに適用されません。これが私のコードです:
var mould = {
partyBox : $.parseHTML('<select name="mouldedParty"><option value="-1" selected disabled>Select Party</option></select>'),
init : function (){ },
process : function (container) {
var pBox = $(mould.partyBox);
var pBoxes = $(container).find('[data-what=partyBox]');
pBox.css({
'padding' : '10px',
'border' : '1px solid #ccc',
'background' : '#368EE0',
'color' : 'white',
'cursor' : 'pointer'
});
$(pBoxes).each(function(index, elem){
var attributes = elem.attributes;
var test = $(elem).replaceWith(pBox);
test.attributes = attributes;
});
// pBoxes.replaceWith(pBox);
}
};
mould.process('body');
このコードの何が問題なのか誰か教えてください。これらの行を置換に使用したにもかかわらず、span タグのすべての属性がドロップダウンに適用されないのはなぜですか
var attributes = elem.attributes;
var test = $(elem).replaceWith(pBox);
test.attributes = attributes;