javascript - 如果 <p contenteditable> 增长,则让 <div> 增长

标签 javascript html css contenteditable

当我输入内容时,如何移动 p 段落下方的所有元素,即 contenteditable

这是我的代码:

body,
html {
  margin: 0;
  padding: 0;
  background-color: white;
}
.input {
  border: 1px dotted black;
  width: 30%;
  float: right;
  left: 45%;
  height: 40px;
  position: absolute;
  background-color: #8CE1FF;
  color: #6E6E6E;
  font-size: 16px;
  font-family: 'Arimo';
}
.input:hover {
  border: 1px solid black;
}
#content {
  border: 1px dotted black;
  width: 50%;
  float: right;
  position: absolute;
  background-color: #8CE1FF;
  color: #6E6E6E;
  font-size: 19px;
  font-family: 'Arimo';
  height: 40%;
}
#content:hover {
  border: 1px solid black;
}
.details {
  margin-bottom: 30px;
  z-index: 900;
  font-family: 'Arimo';
  font-size: 17px;
}
section {
  background-color: #53CFFC;
  margin: 20%;
  z-index: 901;
  border: 1px solid black;
}
#button {
  color: #C0C0C0;
  margin-top: 5%;
  background: #168CB7;
  border: 1px solid #0C8FBF;
  height: 40px;
  width: 100%;
  font-family: 'Arimo';
  font-size: 20px;
  position: relative;
}
#title {
  height: 40px;
  text-align: center;
  font-family: 'Arimo';
  font-size: 20px;
  background: #168CB7;
  color: #C0C0C0;
}
#form {
  padding: 4%;
}
<section>
  <div id="title">title</div>
  <div id="form">
    <div class="details">Name <strong>*</strong>:
      <input type="text" class="input" id="name">
      <br>
    </div>
    <div class="details">Mail <strong>*</strong> :
      <input type="text" class="input" id="mail">
      <br>
    </div>
    <div class="details">Website:
      <input type="text" class="input" id="name">
      <br>
    </div>
    <br>
    <div class="details">
      Text:
      <p id="content" contenteditable="true">Click to write your post.</p>
      <br>
    </div>
  </div>
  <button id="button" onclick="">Submit</button>
</section>

这是一个jsFiddle

最佳答案

  • word-wrap: break-word 添加到 #content
  • #content中将position:absolute更改为relative

body {
  margin: 0;

}
.input {
  border: 1px dotted black;
  width: 30%;
  float: right;
  left: 45%;
  height: 40px;
  position: absolute;
  background-color: #8CE1FF;
  color: #6E6E6E;
  font-size: 16px;
  font-family: 'Arimo';
}
.input:hover {
  border: 1px solid black;
}
#content {
  border: 1px dotted black;
  width: 100%;
  float: right;
  position: relative;
  background-color: #8CE1FF;
  color: #6E6E6E;
  font-size: 19px;
  font-family: 'Arimo';
  height: 40%;
  word-wrap: break-word
}
#content:hover {
  border: 1px solid black;
}
.details {
  margin-bottom: 30px;
  z-index: 900;
  font-family: 'Arimo';
  font-size: 17px;
}
section {
  background-color: #53CFFC;
  margin: 20%;
  z-index: 901;
  border: 1px solid black;
}
#button {
  color: #C0C0C0;
  margin-top: 5%;
  background: #168CB7;
  border: 1px solid #0C8FBF;
  height: 40px;
  width: 100%;
  font-family: 'Arimo';
  font-size: 20px;
  position: relative;
}
#title {
  height: 40px;
  text-align: center;
  font-family: 'Arimo';
  font-size: 20px;
  background: #168CB7;
  color: #C0C0C0;
}
#form {
  padding: 4%;
}
<section>
  <div id="title">title</div>

  <div id="form">

    <div class="details">Name <strong>*</strong>:
      <input type="text" class="input" id="name">
      <br>
    </div>
    <div class="details">Mail <strong>*</strong> :
      <input type="text" class="input" id="mail">
      <br>
    </div>
    <div class="details">Website:
      <input type="text" class="input" id="name">
      <br>
    </div>

    <br>
    <div class="details">Text:
      <p id="content" contenteditable="true">Click to write your post.</p>
      <br>
    </div>

  </div>


  <button id="button" onclick="">Submit</button>
</section>

关于javascript - 如果 <p contenteditable> 增长,则让 <div> 增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501285/

相关文章:

html - twitter-bootstrap row-fluid 不会根据它的 child 大小调整高度

jquery - Div 宽度 100%,即使屏幕扩展超过初始全宽

javascript - 如何过滤表格,在json数据中找到匹配的条目并添加相应的链接

javascript - Django 删除确认

javascript - 使用 html 链接启动 .exe [firefox]

javascript - 使用 2 种形式的 SQL 查询更新 DIV

javascript - 更改页脚图像大小会取代相邻图像

javascript - 确定何时停止修改 div 标签

javascript - 向全局对象添加属性/方法?

javascript - 使用 AS3 或 JS/Jquery 在 SWF 动画结束时执行 JavaScript 函数