我试图学习制作一个简单的 jquery 插件。
这是我的代码:
$.fn.TestPlugin = function( options ) {
var setting = $.extend({
"image" : [ "image1.jpg", "image2.jpg", "image2.jpg"],
"select": [
{
"Label 1": ["opt 1", "opt 2", "opt 3"],
"Label 2": ["opt 4", "opt 5", "opt 6", "opt 7"]
}
]
}, options );
return this.each(function( index ){
$( this ).wrap( "<div class='container' id='container-"+ [index+1] +"'></div>" );
//select
var $select = "<select>";
//loop for select
$select += "</select>";
$( $select ).appendTo( "#container-"+ [index+1] +"" );
});
};
我怎样才能得到这样的结果:
<select>
<optgroup label="Label 1">
<option>opt 1</option>
<option>opt 2</option>
<option>opt 3</option>
</optgroup>
<optgroup label="Label 2">
<option>opt 4</option>
<option>opt 5</option>
<option>opt 6</option>
<option>opt 7</option>
</optgroup>
</select>
产生于setting.select
谢谢
抱歉,我的英语很难理解,我使用谷歌翻译。