如何将Title属性添加到动态填充的<select> </select>

时间:2013-02-06 10:47:24

标签: javascript jquery html

function dynAddOption(list, elementToBeAdded){
    if( list.length > 0) {
        for(var index = 0;  index < list.length; index ++){
            elementToBeAdded.options[index + 1]  = new Option(list[index].description, list[index].id);
        }
    }
}

如何为每个<option>提供工具提示?

1 个答案:

答案 0 :(得分:3)

this question可以看出,您只需要添加title属性即可。在这种情况下,这应该有效:

function dynAddOption(list, elementToBeAdded){
    if (list.length > 0) {
        for(var index = 0; index < list.length; index ++) {
            var opt = new Option(list[index].description, list[index].id);
            opt.title = 'Foo'; // < this is the tooltip
            elementToBeAdded.options[index + 1] = opt;
        }
    }
}