Div onkeydown处理

时间:2013-06-02 18:54:47

标签: javascript onkeypress

在我的代码中,我使用此代码在div上创建div?

主页:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script type="text/javascript">
    function Set() {
        document.getElementById("maindiv").designMode = "On";
    }
</script>
<script src="Scripts/RichTextBox.js" type="text/javascript"></script>
    <title></title>
</head>

<body onload="Set()">
<div id="maindiv" contenteditable="true" onclick="javascript:mainDivclick()" style="width: 500px; height: 170px; border-style: solid; text-align: left; font-size: 17px; font-family: Arial; overflow: scroll;"></div>
</body>
</html>

RichTextBox.js

var childclick = false;

function mainDivclick() {

    if (!childclick) {
        insertDiv(document.getElementById("maindiv")); //create sub div in main div
    }
    childclick = false;
}

function insertDiv(divobj) {

    var num = maindiv.getElementsByTagName('div').length; //count number of divs in maindiv
    var divname = "subdiv" + num; //name for a new div
    var newFrame = document.createElement("div");
    newFrame.setAttribute("name", divname);
    newFrame.onclick = function () {  childclick = true; }; //check clicked
    newFrame.addEventListener("keyup", function () { alert('debugger'); }, false);
    newFrame.style.float = "left";
    newFrame.style.width = 'auto';
    newFrame.style.display = 'inline-block';
    newFrame.style.margin = 0;
    newFrame.style.padding = 0;
    newFrame.style.height = 20 + 'px';
    newFrame.style.border = "10px solid #6e6f6e";
    divobj.appendChild(newFrame);
    newFrame.contentEditable = 'true';
    newFrame.designMode = "On";
    newFrame.focus();
    setSelection(newFrame, 0); //move cursor to new div
}

由于某些原因,没有一个断点在按键时没有触发(我只需要1个onkeypress事件,我只是显示我尝试过的)

0 个答案:

没有答案