0

在我的代码中,我正在使用此代码在 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>

富文本框.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 事件,我只是展示我尝试过的内容)

4

0 回答 0