2015-02-11 85 views
-4

下午好。告訴我如何禁用翻譯本身的重點元素?通過點擊div,它不應該把自己的焦點。謝謝U.對焦禁用對話框

https://jsfiddle"dot"net/ironviper/boyorkdy/ 

如果你點擊鼠標,在「我的文字在這裏」,然後點擊「CLECK我,我按鈕」,那麼焦點將繼續留在場上「我的文字在這裏」。 如果您在「我的文字在這裏」單擊鼠標,然後單擊「CLECK ME我是div」是焦點離開字段「我的文字在這裏」。 當我點擊「CLECK ME i div div」時,如何禁用「我的文字在這裏」字段的焦點?

+0

「setEndOfContenteditable」功能,請提供問題 – 2015-02-11 08:10:38

+0

你在談論的演示?編輯你的文章並添加一些你已經嘗試過的代碼。 – treegarden 2015-02-11 08:19:36

+0

我們不能涉及太多 – 2015-02-11 08:20:17

回答

0

您可以將焦點bact設置爲可編輯div。 我發現從How to move cursor to end of contenteditable entity

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
    <div id="editId" contenteditable="true">My text here</div> 
    <div id="myDiv" onclick="myclick();">CLICK ME i'm div</div> 
</body> 
<script> 
    function myclick() { 
     var el = document.getElementById("editId"); 
     el.focus(); 
     setEndOfContenteditable(el); 
    } 

    function setEndOfContenteditable(contentEditableElement) { 
     var range, selection; 
     if (document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+ 
     { 
      range = document.createRange();//Create a range (a range is a like the selection but invisible) 
      range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range 
      range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start 
      selection = window.getSelection();//get the selection object (allows you to change selection) 
      selection.removeAllRanges();//remove any selections already made 
      selection.addRange(range);//make the range you have just created the visible selection 
     } 
     else if (document.selection)//IE 8 and lower 
     { 
      range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible) 
      range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range 
      range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start 
      range.select();//Select the range (make it the visible selection 
     } 
    } 
</script> 
</html>