📌  相关文章
📜  在选择选项中添加属性 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:20.384000             🧑  作者: Mango

代码示例1


// JavaScript using jQuery
$(function(){
    $('select').change(function(){
       var selected = $(this).find('option:selected');
       var extra = selected.data('foo'); 
       ...
    });
});

// Plain old JavaScript
var sel = document.getElementById('select');
var selected = sel.options[sel.selectedIndex];
var extra = selected.getAttribute('data-foo');