Trên mouseUp
hoặc touchEnd
Tôi muốn nhận tham chiếu về yếu tố mà sự kiện đó đã xảy ra.Làm thế nào để có được touchEnd để cư xử giống như mouseUp?
Trong ví dụ này, bạn cần phải bấm vào một yếu tố, kéo chuột và phát hành trên các yếu tố khác. Khi chuột được nhả, nền của phần tử đó sẽ chuyển thành màu đỏ. Mã của tôi hoạt động chính xác cho các sự kiện chuột, nhưng không hoạt động trên các thiết bị cảm ứng.
Khi touchStart
ở trên một phần tử và ngón tay được giải phóng trên phần tử khác, tôi chỉ nhận được tham chiếu đến phần tử đầu tiên.
Điều tôi cần thay đổi để làm cho sự kiện chạm hoạt động chính xác như sự kiện chuột?
var isMouseDown = false;
var panel1 = document.getElementById('panel1');
var panel2 = document.getElementById('panel2');
panel1.onmousedown = onDocumentMouseDown;
panel1.onmouseup = onDocumentMouseUp;
panel1.onmousemove = onDocumentMouseMove;
panel1.addEventListener('touchstart', onDocumentTouchStart, false);
panel1.addEventListener('touchmove', onDocumentTouchMove, false);
panel1.addEventListener('touchend', onDocumentTouchEnd, false);
panel2.onmousedown = onDocumentMouseDown;
panel2.onmouseup = onDocumentMouseUp;
panel2.onmousemove = onDocumentMouseMove;
panel2.addEventListener('touchstart', onDocumentTouchStart, false);
panel2.addEventListener('touchmove', onDocumentTouchMove, false);
panel2.addEventListener('touchend', onDocumentTouchEnd, false);
function onDocumentMouseDown(event) {
event.preventDefault();
panel1.style.background="#2E442E";
panel2.style.background="#5D855C";
}
function onDocumentMouseUp(event) {
event.preventDefault();
this.style.background="#ff0000";
}
function onDocumentMouseMove(event) {
}
function onDocumentTouchStart(event) {
event.preventDefault();
panel1.style.background="#2E442E";
panel2.style.background="#5D855C";
}
function onDocumentTouchMove(event) {
}
function onDocumentTouchEnd(event) {
event.preventDefault();
this.style.background="#ff0000";
}
thiết bị cảm ứng nào bạn đã thử (có nghĩa là) – shareef
** Câu hỏi tương tự ** [Làm cách nào để tìm ra sự kiện thực tế.tiêu chí của sự kiện chạm vào javascript?] (Http://stackoverflow.com/q/3918842/351708) – Rohit