2012-03-29 30 views
5

Chúng tôi đang phát triển Web-App cho trình duyệt di động, chúng tôi muốn văn bản của bất kỳ hộp nhập liệu nào được chọn bất cứ khi nào hộp đầu vào lấy tiêu điểm. Câu trả lời dưới đây khắc phục sự cố cho trình duyệt Chrome/safari trên máy tính để bàn. Dưới đây giải pháp hoạt động tốt trên trình duyệt của Android, nhưng không phải trên các trình duyệt iPhone/iPad, bất kỳ giải pháp ..Chọn văn bản trên tiêu điểm bằng cách sử dụng jQuery không hoạt động trên iPhone iPad Trình duyệt

$("#souper_fancy").focus(function() { $(this).select() }); 

$("#souper_fancy").mouseup(function(e){ 
     e.preventDefault(); 
}); 

Ref: -

Selecting text on focus using jQuery not working in Safari and Chrome

+3

Hãy thử http://stackoverflow.com/questions/3272089/programmatically-selecting-text-in-an-input-field-on-ios- thiết bị di động-safari – robocat

+1

có. Cảm ơn Robcat, nó đã làm việc giải pháp được tìm thấy ở đây - http://stackoverflow.com/questions/3272089/programmatically-selecting-text-in-an-input-field-on-ios-devices-mobile-safari –

Trả lời

4

Trả lời tìm thấy ở đây không biết cách đánh dấu câu hỏi này là trùng lặp. Programmatically selecting text in an input field on iOS devices (mobile Safari)

sửa chữa của tôi nhìn như thế này: -

<script type="text/javascript"> 
      $('div,data-role=page').live('pageshow', function (event) { 
       jQuery.validator.unobtrusive.parse(".ui-page-active form"); 
       $("input[type='text'], textarea, input[type='password'], input[type='number']").live('mouseup', function (e) { e.preventDefault(); }); 
       $("input[type='text'], textarea, input[type='password'], input[type='number']").live('focus', function() {this.setSelectionRange(0, 9999); });    
      });  
    </script> 
1

Có thể thử này:

vmouseup
Sự kiện chuẩn hóa để xử lý sự kiện chạm hoặc di chuột

JS:

$("#souper_fancy").focus(function() { 
    $(this).select(); 
}); 

$("#souper_fancy").vmouseup(function(e){ 
    e.preventDefault(); 
}); 
+0

Nó không hoạt động :( –

-1

một này làm việc cho tôi.

 $("input[type='text'],select,textarea").focus(function() { 
      $(this).addClass('text-focus'); 
     }); 

     $("input[type='text'],select,textarea").blur(function() { 
      $(this).removeClass('text-focus'); 
     }); 

     .text-focus 
     { 
     border: 1px solid #ea9a00; 
     } 
1

Bạn có thể sử dụng document.execCommand('selectAll');. Tuy nhiên, nếu người dùng cuộn trang, bạn sẽ nhận được menu sao chép/dán hiển thị.

Đây là những gì tôi sử dụng:

function trySelect(el, evenInactive, select_ios) { 
    var select = function() { 
     try { 
      if (mojo.isTouch && select_ios && core.iosDevice && mojo.isInput(el) && document.execCommand) { 
       document.execCommand('selectAll'); 
      } else { 
       el.select(); 
      } 
     } catch (e) { 
     } 
    }; 
    if (el && el.select && !el.disabled && (!el.readOnly || el.selectReadOnly) && mojo.isInput(el)) { 
     if (evenInactive || mojo.activeElement() === el) { 
      if (mojo.isTouch && core.webkitVer) { // http://code.google.com/p/chromium/issues/detail?id=32865 
       setTimeout(select, 0); 
      } else { 
       select(); 
      } 
     } 
    } 
} 

Đó tham chiếu một số chức năng nội bộ:

  • mojo.isTouch - đúng đối với một thiết bị cảm ứng
  • core.iosDevice - đúng đối với một thiết bị iOS
  • mojo.isInput - kiểm tra cho phần tử đầu vào
  • mojo.activeElement() - document.activeElemen t

không được sử dụng và el.setSelectionRange(0, el.value.length); được sử dụng thay thế. Điều đó dường như hoạt động tốt trên iOS5 ... Nó có thể không hoạt động trên iOS4 (Tôi không có thiết bị iOS4 để kiểm tra).

Các vấn đề liên quan