Các câu trả lời được liệt kê một phần theo tôi. Tôi đã liên kết dưới đây hai ví dụ về cách làm điều này trong Angular và với JQuery.
Giải pháp này có các tính năng sau:
- trình cho tất cả các trình duyệt hỗ trợ JQuery, Safari, Chrome, IE, Firefox, vv
- trình cho PhoneGap/Cordova: Android và iOS.
- Chỉ chọn tất cả một lần sau khi nhập được lấy nét cho đến khi làm mờ tiếp theo và sau đó lấy nét
- Có thể sử dụng nhiều đầu vào và không bị trục trặc.
- chỉ thị góc có lớn tái sử dụng chỉ cần thêm chỉ thị chọn-tất cả-on-click
- JQuery có thể được sửa đổi một cách dễ dàng
JQuery: http://plnkr.co/edit/VZ0o2FJQHTmOMfSPRqpH?p=preview
$("input").blur(function() {
if ($(this).attr("data-selected-all")) {
//Remove atribute to allow select all again on focus
$(this).removeAttr("data-selected-all");
}
});
$("input").click(function() {
if (!$(this).attr("data-selected-all")) {
try {
$(this).selectionStart = 0;
$(this).selectionEnd = $(this).value.length + 1;
//add atribute allowing normal selecting post focus
$(this).attr("data-selected-all", true);
} catch (err) {
$(this).select();
//add atribute allowing normal selecting post focus
$(this).attr("data-selected-all", true);
}
}
});
góc: http://plnkr.co/edit/llcyAf?p=preview
var app = angular.module('app', []);
//add select-all-on-click to any input to use directive
app.directive('selectAllOnClick', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var hasSelectedAll = false;
element.on('click', function($event) {
if (!hasSelectedAll) {
try {
//IOs, Safari, thows exception on Chrome etc
this.selectionStart = 0;
this.selectionEnd = this.value.length + 1;
hasSelectedAll = true;
} catch (err) {
//Non IOs option if not supported, e.g. Chrome
this.select();
hasSelectedAll = true;
}
}
});
//On blur reset hasSelectedAll to allow full select
element.on('blur', function($event) {
hasSelectedAll = false;
});
}
};
}]);
Nguồn
2015-09-10 12:13:49
Một cách tiếp cận tốt hơn là sử dụng một '
Hoặc thậm chí sử dụng trình giữ chỗ nếu bạn đang làm việc trong một dự án HTML5 hiện đại. –