mousedown event on options in select is not working in IE, Is there any workaround?

前端 未结 2 1404
梦如初夏
梦如初夏 2021-01-24 02:38

This following code snippet is to avoid the need for ctrl-click in a multi-select box

but it does not work in IE 8 .

Is there any work around to achive the same

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 02:56

    I found one major issue with jQuery answer above. The .val() of the $(select) won't update.

    Here is working solution:

    $select.mousedown(function (e) {
        e.preventDefault();
        const select = this;
        const { scrollTop } = select;
        e.target.selected = !e.target.selected;
        setTimeout(function () {
            select.scrollTop = scrollTop;
        }, 0);
    }).mousemove(function (e) { e.preventDefault(); });
    

提交回复
热议问题