dtll2016 2017-11-01 11:30
浏览 125
已采纳

可信的div高度限制

Hey guys I have problems with contenteditable height. With code like this:

 function Preview() {
 var x = document.getElementById("code").value;
 document.getElementById("preview").innerHTML = x;
}
  .preview{
text-align: left;
color: white;
margin-left: 10px;
max-height: 300px;
overflow: auto;
}
<section id="main"> 
   <div id="gra"> 
      <input type="hidden" id="current-code" name="current-code" />
         <textarea id="code" name="code">
             <!DOCTYPE html>
               <html lang="en">
                  <head>
                    <meta charset="UTF-8">
                    <title>A Simple PHP File</title>
                  </head>
                  <body>
                    <h1><?php echo "Hello, world!"; ?></h1>
                  </body>
               </html> 
             </textarea>
       <div class="flex">
          <a href="#" class="myButton" onclick="Preview()">Preview</a>
         <a href="#" class="myButton" onclick="Reset()">Reset</a>  
       </div>
      </div>
      <div id="gra">
        <div contenteditable="true" class="preview" id="preview">  
      </div>
    </div>
 </section>

When I enter code with more I can observe effect like this: test

Anyone can help me with limit contenteditable height?

</div>
  • 写回答

1条回答 默认 最新

  • dongyanju1094 2017-11-01 11:50
    关注

    try this code

    and also change color: red; instead of color:white because its not see anything on white screen

    function Preview() {
    
      var x = document.getElementById("code").value;
      document.getElementById("preview").innerHTML = x;
    }
    
    function Reset() {
      document.getElementById("preview").innerHTML = "";
    }
    .preview {
      text-align: left;
      color: red;
      margin-left: 10px;
      max-height: 300px;
      overflow-y: scroll;
      border: solid 1px #ddd;
    }
    <section id="main">
      <div id="gra">
        <input type="hidden" id="current-code" name="current-code" />
        <textarea id="code" name="code">
                 <!DOCTYPE html>
                   <html lang="en">
                      <head>
                        <meta charset="UTF-8">
                        <title>A Simple PHP File</title>
                      </head>
                      <body>
                        <h1><?php echo "Hello, world!"; ?>you write php code hear so it will not print on html</h1>
                      </body>
                   </html> 
                 </textarea>
        <div class="flex">
          <a href="#" class="myButton" onclick="Preview()">Preview</a>
          <a href="#" class="myButton" onclick="Reset()">Reset</a>
        </div>
      </div>
      <div id="gra">
        <div contenteditable="true" class="preview" id="preview">
        </div>
      </div>
    </section>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵