可信的div高度限制

时间:2017-11-01 11:30:12

标签: php css html5 contenteditable

嘿伙计们,我有高低的问题。使用这样的代码:



 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>
&#13;
&#13;
&#13;

当我输入更多代码时,我可以观察到这样的效果: test

任何人都可以帮助我限制满足的高度?

1 个答案:

答案 0 :(得分:1)

试试这段代码

并且还更改color: red;而不是color:white,因为它在白色屏幕上看不到任何内容

&#13;
&#13;
function Preview() {

  var x = document.getElementById("code").value;
  document.getElementById("preview").innerHTML = x;
}

function Reset() {
  document.getElementById("preview").innerHTML = "";
}
&#13;
.preview {
  text-align: left;
  color: red;
  margin-left: 10px;
  max-height: 300px;
  overflow-y: scroll;
  border: solid 1px #ddd;
}
&#13;
<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>
&#13;
&#13;
&#13;