div上的焦点禁用

时间:2015-02-11 08:09:12

标签: javascript html css

下午好。告诉我如何禁用翻译本身的元素?通过点击div它不应该转换自己的焦点。谢谢你。

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

如果您在"我的文字在这里点击鼠标",然后点击" CLECK ME i' m按钮"然后焦点将留在现场"我的文字在这里"。 如果你在"我的文字在这里"中单击鼠标,然后点击" CLECK ME i' m div"重点是离开现场"我的文字在这里"。 如何禁用远离现场的焦点"我的文字在这里"当你点击" CLECK ME i' m div"?

1 个答案:

答案 0 :(得分:0)

您可以将焦点设置回可编辑的div。 我发现' setEnvif Contenteditable n#39;函数来自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>