2015-06-10 15 views
2

所以我環顧四周,因爲我需要解決iOS錯誤,而且我的模擬點擊似乎都不起作用。我試圖手動專注於一個textarea元素,但要做到這一點,我需要手動模擬點擊上述textarea元素。因此,這裏是我已經試過:JS/jQuery - 在textarea元素上強制單擊

$('#text-input')[0].click(); 
$('#text-input').click(); 
$('#text-input').trigger('click'); 
$('#text-input').focus(); 

$('#text-input').on('click', function { 
    $(this).focus(); 
}); 

$('#text-input').trigger('click'); 

$('#text-input').on('touchstart', function { 
    $(this).focus(); 
}); 

$('#text-input').trigger('touchstart'); 

HTML:

<textarea id="comment-input" class="comment-input" rows="1" maxlength="{{maxTextLength}}" ng-model="newMessage.content" placeholder="{{ messagePlaceholder }}" /> 

爲什麼這不工作有什麼想法?

+0

我們可以看到HTML? –

+0

似乎是一個錯誤。參考[1](http://stackoverflow.com/questions/979309/how-do-i-focus-an-html-text-field-on-an-iphone-causing-the-keyboard-to-come-up )和[2](https://github.com/jquery/jquery-mobile/issues/3016) – Drakes

+0

@Drakes我知道這是一個錯誤,我試圖爲它做一個解決方法。 – germainelol

回答

0

嘗試.focus,而不是像click

$('#text-input').trigger('focus'); 

或只是

$('#text-input').focus(); 

UPDATE

你可以嘗試setTimeout,在大多數情況下,工作

$("#text-input").on("click", function(event) { 
    setTimeout(function() { 
     $(event.target).select(); //or $(event.target).focus(); 
    }, 1); 
}); 

更新2

您也可以嘗試使用touchstart事件像

//trigger touch on element to set focus 
$("#text-input").trigger('touchstart'); 

//event handler to set the focus() 
$('#text-input').on('touchstart', function() { 
    $(this).focus(); // inside this function the focus works 
}); 
+0

我試過使用'。焦點()'已經和它不工作,因爲它是一個iOS錯誤。我意識到''.on'有一個錯字,但是這個錯誤並沒有在編碼時產​​生:P – germainelol

+0

人們將源代碼複製並粘貼到SO,這使我們認爲當一切看起來很好時,他們錯過了某種語法。: )@germainelol –

+0

@germainelol更新了答案..請檢查並讓我知道。 –