如何附加固定在生成的contentEditables div顶部和底部的nonEditableContent?

我试图在内容可编辑= true的动态生成的div上“模拟”固定且不可编辑的页眉页脚

首先,我尝试使用此css放置页眉和页脚:

.header{
                /*margin:-100px 0px;*/
                margin-left: -2cm;
                background-color: red;
                vertical-align:middle; 
                text-align:center;
            }
            .footer{
                background-color: darkgray;
                margin-top: 735px;
                height: 100px;
                margin-left: -2cm;
            }

并尝试启用首页上的标题,效果很好:

<div id="editor">
            <div contenteditable="true" class="page" id="page-1">
                <div class="header" contenteditable="false">
                    <img class="brasao" alt="brasao-rj*" src="https://i.imgur.com/ktmBQCS.png" />
                    <span>Procuradoria Geral do Estado do Rio de Janeiro</span>
                </div>
                <b>hello</b>
                <div class="footer" contenteditable="false">
                    Rua Carmo do Cajuru 128
                </div>
            </div>
        </div>

但是页脚未按预期工作,因为用户可以按下页脚以在div上放置更多内容。

在目前的方法中,我尝试在调用newPage.focus()之前附加“ header” div。但不幸的是,该行为与预期不符,从而使用户无法同时按下页眉和页脚。

const getHeader = () => {
    let header = document.createElement('div');
    header.setattribute('contenteditable',false);
    header.setattribute('class','header');
    let imgBrasao = document.createElement('img');
    imgBrasao.class = 'brasao';
    imgBrasao.setattribute('class','brasao');
    imgBrasao.src = 'https://i.imgur.com/ktmBQCS.png';
    let spanPGE = document.createElement('span');
    spanPGE.textContent = 'Procuradoria Geral do Estado do Rio de Janeiro';
    header.appendChild(imgBrasao);
    header.appendChild(spanPGE);
    return header;
};    
const getFooter = () => {
    let footer = document.createElement('div');
    footer.setattribute('contenteditable',false);
    footer.setattribute('class','footer');
    let spanPGE = document.createElement('span');
    spanPGE.textContent = 'Rua Carmo do Cajuru 128 - Centro RJ';
    footer.appendChild(spanPGE);
    return footer;
};

完整代码在这里:

https://jsitor.com/ETmvUUXaS

(不带页眉和页脚的版本:https://jsitor.com/9J30B6YfG

那么,如何在这些内容可编辑的div上模拟页眉和页脚?

谢谢!

study222 回答:如何附加固定在生成的contentEditables div顶部和底部的nonEditableContent?

我设法做到了没有jQuery。概念是在内容周围添加包装器,然后向该包装器而不是页面添加事件。另外,我创建了一个模板HTML页面,因此无需进行清理即可克隆代码。

为使页脚位于底部,我将页面显示更改为0xffff7d77,将$ ./bin/freadunsignedval ../dat/100000int.bin 7d77 6cad c544 21f8 723f 54d1 8a81 2c6a 1ba9 f95b 1858 7565 f4b 28e4 7fdd 5a92 b5df 7a3f 4e1a 7e19 669 f365 34c0 95e 903 689d 66f2 abf2 1223 1290 372f f9b 7f3d 71eb ce6d 717c 46bc 2712 1de6 6265 d248 363e 57cb 3d03 5f23 57a8 1795 2944 51e7 65af 275d 5851 724a 5c1e 61af 7b4d 44bb 48a2 4f5b 56de 5b32 68b 6679 5a6f 7876 180c 4beb 3f33 3f1f 69d1 2198 6cd7 200f 7963 29da 7f32 510b 4170 2877 22f3 271f 4fd4 84bc 196a 2bf2 5cf3 14b7 70ad 2595 6413 ... 6503 b2 f135 15f6 776c b7f3 1ffd 1365 1e4d 129b 23f 6c3e 20c a8c 2ef6 f72b 4d4 793a 1b6b 425 79d5 6bac ba8 6527 6239 17ea 644e 1175 4464 1c88 346d 2967 1d3a 4339 3f5d 14a6 b46 5f5a 更改为flex。然后将flex-direction设置为页脚。

代码如下:

column
margin-top: auto
function redator(divId) {
  const root = document.getElementById(divId)
  const a4 = {
    height: 830,width: 595,lineHeight: 30
  };
  const template = document.querySelector('#template');
  const headerHeight = root.querySelector('#page-1 .header').offsetHeight;
  const footerHeight = root.querySelector('#page-1 .footer').offsetHeight;

  const getChildrenHeight = (element) => {

    total = parseFloat(getComputedStyle(element).paddingBottom);

    if (element.childNodes) {
      for (let child of element.childNodes) {
        switch (child.nodeType) {
          case Node.ELEMENT_NODE:
            total += child.offsetHeight;
            break;
          case Node.TEXT_NODE:
            let range = document.createRange();
            range.selectNodeContents(child);
            rect = range.getBoundingClientRect();
            total += (rect.bottom - rect.top);
            break;
        }
      }
    }
    return total;
  };

  const getPageHeight = (content) => {
    const children = getChildrenHeight(content);
    return children + headerHeight + footerHeight;

  };

  const setSelection = (node,offset) => {
    let range = document.createRange();
    let sel = window.getSelection();
    range.setStart(node,offset);
    range.collapse(true);
    sel.removeAllRanges();
    sel.addRange(range);
  };

  const addPage = () => {

    const newPage = template.cloneNode(true);
    const pages = root.getElementsByClassName('page');

    newPage.id = 'page-' + (pages.length + 1);
    newPage.className = 'page';
    root.appendChild(newPage);
    newPage.querySelector(".content").focus();
    newPage.querySelector(".content").addEventListener('input',onInput);

    newPage._emptyPage = true;
    return newPage.querySelector(".content");
  };

  function onInput(e) {
    const content = this;
    const page = this.closest(".page")
    const previousPage = page.previousElementSibling;
    const nextPage = page.nextElementSibling;

    const pageHeight = getPageHeight(content);
    const lastChild = content.lastChild;
    const cloneChild = lastChild.cloneNode(true);
    const textContent = content.innerText;
    if ((pageHeight === 0 || textContent.length <= 1) && !!previousPage && !page._emptyPage) {
      page.remove();
      previousPage.querySelector(".content").focus();
      const lastChild = previousPage.querySelector(".content").lastChild;
      setSelection(lastChild,lastChild.childNodes.length);
    } else if (pageHeight > a4.height && !nextPage) {
      lastChild.remove();
      addPage().appendChild(cloneChild);
    } else if (pageHeight > a4.height && nextPage) {

      lastChild.remove();
      nextPage.querySelector(".content").insertBefore(cloneChild,nextPage.querySelector(".content").firstChild);
      let selection = getSelection().getRangeAt(0).startContainer.parentElement.closest('div');
      if (selection === page.lastChild) {
        setSelection(cloneChild,0);
      }
    } else if (pageHeight < a4.height - a4.lineHeight && !!nextPage) {
      let firstChildOfNextPage = nextPage.firstChild;
      let clone = firstChildOfNextPage.cloneNode(true);
      firstChildOfNextPage.remove();
      page.appendChild(clone);
    }
    page._emptyPage = false;
  }

  document.execCommand("DefaultParagraphSeparator",false,"div");
  root.querySelector('#page-1 .content').addEventListener('input',onInput);
}

document.addEventListener('DOMContentLoaded',function() {
  redator('editor');
},false);
redator('editor');

,

仅制作.body元素contentEditable,并在超过一定高度时将其设置为false:

let bodyMHeight = $(".page").height() - $(".header").height() - $(".footer").height();

$(".body").on("input",function() {
   //console.log($(".body").height(),bodyMHeight);
   if($(".body").height() > bodyMHeight){
      //console.log("over");
      $(this).attr("contenteditable","false");
   }else{
      //console.log("not over");
   }
});
body{
  margin: 0;
}

.page{
   display: flex;
   flex-flow: column;
   height: 100vh;
   min-height: 250px;
}

.header {
  background-color: red;
  text-align: center;
}

.header img {
   height: 100px;
}

.body{
  flex: 1;
  overflow-wrap: break-word;
}

.footer {
  background-color: darkgray;
  min-height: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="editor">
  <div class="page" id="page-1">
    <div class="header">
      <img class="brasao" alt="brasao-rj*" src="https://i.imgur.com/ktmBQCS.png" />
      <span>Procuradoria Geral do Estado do Rio de Janeiro</span>
    </div>
    <div class="body" contenteditable="true"> 
        <b>hello</b>
    </div>
    <div class="footer">
      Rua Carmo do Cajuru 128
    </div>
  </div>
</div>

本文链接:https://www.f2er.com/3063154.html

大家都在问