Vui lòng mở các ví dụ này trên iPhone của bạn.iPhone - JavaScript Dispatch Event - My Select Menu sẽ không mở trên chức năng setTimeout
Mã bên dưới là hành vi đúng.
<select id="selectMenu">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input id="button" name="button" type="button" value="Click Me" />
var initEvt = function (el, type) {
var e = document.createEvent('MouseEvents');
e.initEvent(type, true, true, window);
el.dispatchEvent(e);
},
selMenu;
selMenu = document.getElementById('selectMenu');
document.getElementById('button').onclick = function() {
initEvt(selMenu, 'mouseover');
initEvt(selMenu, 'mousedown');
initEvt(selMenu, 'mouseup');
initEvt(selMenu, 'click');
}
Nếu bạn thêm một hàm setTimeout nó sẽ không hoạt động.
<select id="selectMenu">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input id="button" name="button" type="button" value="Click Me" />
var initEvt = function (el, type) {
var e = document.createEvent('MouseEvents');
e.initEvent(type, true, true, window);
el.dispatchEvent(e);
},
selMenu;
selMenu = document.getElementById('selectMenu');
document.getElementById('button').onclick = function() {
setTimeout(function() {
initEvt(selMenu, 'mouseover');
initEvt(selMenu, 'mousedown');
initEvt(selMenu, 'mouseup');
initEvt(selMenu, 'click');
}, 500);
}
http://jsfiddle.net/ghwfstmj/1/
Bất cứ ý tưởng tại sao?
Tôi cũng thấy sự cố cuộn lạ. Nếu có hai menu được chọn, nó sẽ cuộn xuống nút và sao lưu nếu bạn sử dụng mũi tên trái và phải (tùy chọn chọn). Nếu bạn nhấp vào thực hiện nó không làm điều này hoặc bấm vào chúng bằng tay:
select {
display: block;
width: 100%;
}
#selectMenu { margin-bottom: 50px; }
#selectMenu2 { margin-bottom: 200px; }
<select id="selectMenu">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select id="selectMenu2">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input id="button" name="button" type="button" value="Click Me" />
var initEvt = function (el, type) {
var e = document.createEvent('MouseEvents');
e.initEvent(type, true, true, window);
el.dispatchEvent(e);
},
selMenu;
selMenu = document.getElementById('selectMenu');
document.getElementById('button').onclick = function() {
initEvt(selMenu, 'mouseover');
initEvt(selMenu, 'mousedown');
initEvt(selMenu, 'mouseup');
initEvt(selMenu, 'click');
}
http://jsfiddle.net/ghwfstmj/4/
Cheers!
Dường như mọi thứ đều hoạt động tốt. Điều hẹn giờ đang hoạt động (ít nhất là với chrome). Về hành vi kỳ lạ với mũi tên trái phải, đây có thể chỉ là một điều tập trung. Bạn gửi các nhấp chuột và mouseevent nhưng có lẽ bạn nên kích hoạt tập trung vào selMenu cũng như – Bene
Hãy thử nó trên Safari (iPhone). Trình duyệt và trình giả lập hoạt động tốt. Tôi đã thử thêm trọng tâm và không có may mắn. Bạn có thể đạt được hành vi tương tự nếu bạn thêm một tabindex và sử dụng mã $ ('# selectMenu'). Trigger ('focus'); Điều này có nghĩa là nó không liên quan đến sự kiện trọng tâm. Tôi cũng đã thêm một dispatchEvent phụ cho điều này và không có may mắn: ( – Paddy
Oups, sai lầm của tôi tôi đã đi thẳng lên trong mã và quên toàn bộ phần "iPhone". – Bene