jQuery Focus fails on firefox

前端 未结 4 1131
既然无缘
既然无缘 2020-11-27 07:34

I\'ve been some testing reserching for this other question, when I noticed something very peculiar. FF4/5 fail to fire the focus jQuery event. The other questio

4条回答
  •  粉色の甜心
    2020-11-27 07:47

    The focus seems to work now in the latest Firefox without the need of the setTimeout function.

    If you want to also select the input field you will have to make use of the .select() function though as document.execCommand('SelectAll'); doesn't seem to work on Firefox either.

    So having the input field first focused and then selected you then can copy it or do whatever you want with it.

    In my use case I required to copy the url from the input field if someone pressed on the copy button:

    $(".copyURL").click(function(){ 
        $(this).prev().focus().select();
        document.execCommand("Copy",false,null);
    });
    

    I hope this could help anyone else who's searching for this problem!

提交回复
热议问题