需要準備的材料分別有:電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器,新建html文件,例如:index.html,並引入jquery。
2、在index.html中的<script>標簽,輸入jquery代碼:
$('select option').each(function (i) {
if ($(this).attr('selected')) {
$('body').append(i);
}
});
3、瀏覽器運行index.html頁面,此時用jquery獲取到了option的索引值被打印了出來。