最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Focus inputs on double click? - Stack Overflow

programmeradmin2浏览0评论

I want to prevent input focus onclick and focus inputs on double click.

Something like....

$('input').click(function) {
    $(this)preventFocus();
});

$('input').dblclick(function) {
    $(this)focus();
});

Or maybe I should use an if statement?

I want to prevent input focus onclick and focus inputs on double click.

Something like....

$('input').click(function) {
    $(this)preventFocus();
});

$('input').dblclick(function) {
    $(this)focus();
});

Or maybe I should use an if statement?

Share Improve this question asked Oct 30, 2013 at 20:38 colmtuitecolmtuite 4,49111 gold badges47 silver badges70 bronze badges 5
  • 1 try $(this).blur() instead of $(this)preventFocus() – Howard Renollet Commented Oct 30, 2013 at 20:41
  • Have you actually tried anything? check out: stackoverflow./questions/8735764/… – Mike Edwards Commented Oct 30, 2013 at 20:42
  • Been trying for 2 hours mate :( I'll take a look at that answer, thanks. – colmtuite Commented Oct 30, 2013 at 20:45
  • Sorry to hear that :( You should try to phrase your questions in terms of 1) this is what I did and 2) this is the problem I'm having. The code you posted is not valid Javascript so as written you would get a syntax error, which you didn't mention. – Mike Edwards Commented Oct 30, 2013 at 20:47
  • Ok thanks for tips. Will take them into account in future. – colmtuite Commented Oct 30, 2013 at 21:00
Add a ment  | 

2 Answers 2

Reset to default 5

I guess something like this should work :

$('input').on({
    focus: function() {
        if (!$(this).data('disabled')) this.blur()
    },
    dblclick: function() {
        $(this).data('disabled', true);
        this.focus()
    },
    blur: function() {
        $(this).data('disabled', false);
    }
});

FIDDLE

You could try something like the following

HTML:

<input type="text" id="myInput" readonly="readonly" />

jQuery:

$("#myInput").dblclick(function(){
   $(this).prop("readonly", false)
});
$("#myInput").blur(function(){
   $(this).prop("readonly", true); 
});

See http://jsfiddle/KWuR5/

发布评论

评论列表(0)

  1. 暂无评论