2015-06-08 35 views
2

我在iframe中有一個輸入字段,所以當我點擊任何輸入字段時,它會變得焦點。但是,如果我再次點擊外部(身體區域),iphone鍵盤不會關閉。每當我點擊完成按鈕時,它就會關閉。 iframe中只有iphone和ipad的問題。這將在所有瀏覽器中很好地工作。所以,我嘗試瞭如果沒有焦點,稱爲blur()或document.activeelement。但他們都沒有工作。如何在輸入元素未使用JS進行聚焦時強制關閉ipad/iphone鍵盤?

試過這個,焦點被觸發但焦點沒有被觸發。

document.addEventListener("focusout", function(){ 
     document.activeElement.blur(); 
    }); 

對此有何修復?

sample.html

<!doctype html> 
<html> 
<head> 
</head> 
<body> 
<iframe src="index2.html" width="100%" height="200px"></iframe> 
</body> 
</html> 

index2.html

<!doctype html> 
<html> 
<head> 
<style> 
input:focus{ 
border: 2px solid red; 
} 
</style> 
</head> 
<body> 
<br></br> 
<input type="text" id="myText"/> 
</body> 
</html> 
+0

迪內希?你還沒找到答案 –

+0

@SanKrish:你爲什麼不在這裏發表你的答案:P? –

回答

2

我不知道爲什麼blur()不是裏面的iframe工作,但它的工作沒有iframe中。

所以我做了一些測試,最後我發現他們:

  1. alert()簡單警惕的東西,它會關閉鍵盤。
  2. focus()在非文本元素上,如按鈕。

在這裏,我表明我如何做到這一點使用focus()

sample.html

<!doctype html> 
<html> 
<head> 
</head> 
<body> 
<iframe src="index2.html" width="100%" height="200px"></iframe> 
</body> 
</html> 
<script> 
    document.getElementById("myframe").addEventListener("load",function() 
     { 
      document.getElementById("myframe").contentWindow.document.addEventListener("touchend",touchEnd, false); 
     }, false 
    ); 

    document.addEventListener("touchend",touchEnd, false); 

    function touchEnd(event) { 
     if(event.changedTouches[0].target.id != "mytext") 
     { 
      document.getElementById("myframe").contentWindow.document.getElementById("hidekeyboard").focus(); 
     } 
    } 
</script> 

index2.html

<!doctype html> 
<html> 
<head> 
<style> 
input:focus{ 
border: 2px solid red; 
} 
</style> 
</head> 
<body> 
<br></br> 
<input type="text" id="myText"/> 
<button type="button" id="hidekeyboard" style="width:0px; height:0px; opacity:0;"></button> 
</body> 
</html> 
+0

不工作兄弟!外接錄像時,我無法關閉鍵盤 –

+0

您可以發佈簡單的示例代碼嗎? –

+0

更新了我的代碼,請參閱 –

相關問題