简体   繁体   English

如果在textarea中插入段落,如何设置div的其他位置

[英]How to set a different position of a div if I insert a paragraph in my textarea

I would like to set that if I press enter in my textarea between the words "Write Here" and "Guys", the two boxes with the words "Write" and "Here" should be displayed one next to the other orizontally, and the box with "Guys" should be under the "Write" one. 我要设置的是,如果在文本区域中按“在此处写”和“人”之间按回车键,则应将两个分别带有“写”和“在这里”字样的框相互垂直显示,并且带有“ Guys”的框应在“ Write”下。

And if there are four words with the same example of before, the fourth box should be next to the "Guys" one. 并且如果有四个单词具有与之前相同的示例,则第四个框应位于“人”旁边。

In my code I wrote that all the boxes will display orizontally, if there is space to disply them, because I don't know how to put a box below the others if ther is an "enter space" in the textarea. 在我的代码中,我写道,如果有空间可显示所有框,它们将按正交方向显示,因为如果文本框是“输入空间”,我不知道如何在其他框下方放置一个框。

Whit the code is much easier to understand: 惠特代码更容易理解:

JSfiddle JS小提琴

HTML: HTML:

<div id="faketxt" contenteditable>Write Here
Guys</div>
<button id='btn'>OK</button><br>
<div id='boxes'>

CSS: CSS:

#faketxt {
    -moz-appearance: textfield-multiline;
    -webkit-appearance: textarea;
    border: 1px solid gray;
    height: 28px;
    overflow: auto;
    padding: 2px;
    resize: both;
    width: 400px;
}

.fakes{
  width: 150px;
  height: 300px;
  font-size: 10px;
  border-style: solid;
  display:inline-block;
}

#boxes{
  display : flex;
}

JQuery: jQuery的:

$('#btn').click(function() {
  var primo = document.getElementById('faketxt');
  var wordLimit = 1;
  var words = primo.innerHTML.replace(/(&lt;([^&gt;]+)&gt;)/ig,"").split(/\s/);
  if (words.length) {
    var count = 0;
    var div = createDiv();
    words.forEach(function(word) {
      if (++count > wordLimit) {
        count = 1;
        div = createDiv();
      }
      if (div.innerHTML) {
        div.append(' ');
      }
      div.append(word);
    });
  }
});

function createDiv() {
  div = document.createElement('div');
  div.className = 'fakes';
  document.getElementById('boxes').append(div);
  return div;
}

try this 尝试这个

var words = primo.innerText.replace(/(&lt;([^&gt;]+)&gt;)/ig,"").split(/\s/);

replace below line 替换下面的行

  if (div.innerHTML) {
    div.append(' ');
  }

with

  if (word == '') {
    div.className = '';
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM