如何通过<Input>动态更改JQuery值?

戴夫

下面的小提琴将文本转换为段落,而问题是JQuery函数属性chunkSize = 100;当前定义了要包含的每个分割段落的字符数。

是否有可能将用户能够通过使用一个改变这种动态的<input>并且<button>当用户将能够输入自己想要的字符,每个动态段落,并应用它?

小提琴

如果可以提供一个新的小提琴,那将不胜感激,因为我还是编码的新手。

谢谢你!

$(function() {
  $('select').on('change', function() {
    //Lets target the parent element, instead of P. P will inherit it's font size (css)
    var targets = $('#content'),
      property = this.dataset.property;
    targets.css(property, this.value);
    sameheight('#content p');
  }).prop('selectedIndex', 0);
});
var btn = document.getElementById('go'),
  textarea = document.getElementById('textarea1'),
  content = document.getElementById('content'),
  chunkSize = 100;
btn.addEventListener('click', initialDistribute);
content.addEventListener('keyup', handleKey);
content.addEventListener('paste', handlePaste);

function initialDistribute() {
  var text = textarea.value;
  while (content.hasChildNodes()) {
    content.removeChild(content.lastChild);
  }
  rearrange(text);
}

function rearrange(text) {
  var chunks = splitText(text, false);
  chunks.forEach(function(str, idx) {
    para = document.createElement('P');
    para.classList.add("Paragraph_CSS");
    para.setAttribute('contenteditable', true);
    para.textContent = str;
    content.appendChild(para);
  });
  sameheight('#content p');
}

function handleKey(e) {
  var para = e.target,
    position,
    key, fragment, overflow, remainingText;
  key = e.which || e.keyCode || 0;
  if (para.tagName != 'P') {
    return;
  }
  if (key != 13 && key != 8) {
    redistributeAuto(para);
    return;
  }
  position = window.getSelection().getRangeAt(0).startOffset;
  if (key == 13) {
    fragment = para.lastChild;
    overflow = fragment.textContent;
    fragment.parentNode.removeChild(fragment);
    remainingText = overflow + removeSiblings(para, false);
    rearrange(remainingText);
  }
  if (key == 8 && para.previousElementSibling && position == 0) {
    fragment = para.previousElementSibling;
    remainingText = removeSiblings(fragment, true);
    rearrange(remainingText);
  }
}

function handlePaste(e) {
  if (e.target.tagName != 'P') {
    return;
  }
  overflow = e.target.textContent + removeSiblings(fragment, true);
  rearrange(remainingText);
}

function redistributeAuto(para) {
  var text = para.textContent,
    fullText;
  if (text.length > chunkSize) {
    fullText = removeSiblings(para, true);
  }
  rearrange(fullText);
}

function removeSiblings(elem, includeCurrent) {
  var text = '',
    next;
  if (includeCurrent && !elem.previousElementSibling) {
    parent = elem.parentNode;
    text = parent.textContent;
    while (parent.hasChildNodes()) {
      parent.removeChild(parent.lastChild);
    }
  } else {
    elem = includeCurrent ? elem.previousElementSibling : elem;
    while (next = elem.nextSibling) {
      text += next.textContent;
      elem.parentNode.removeChild(next);
    }
  }
  return text;
}

function splitText(text, useRegex) {
  var chunks = [],
    i, textSize, boundary = 0;
  if (useRegex) {
    var regex = new RegExp('.{1,' + chunkSize + '}\\b', 'g');
    chunks = text.match(regex) || [];
  } else {
    for (i = 0, textSize = text.length; i < textSize; i = boundary) {
      boundary = i + chunkSize;
      if (boundary <= textSize && text.charAt(boundary) == ' ') {
        chunks.push(text.substring(i, boundary));
      } else {
        while (boundary <= textSize && text.charAt(boundary) != ' ') {
          boundary++;
        }
        chunks.push(text.substring(i, boundary));
      }
    }
  }
  return chunks;
}
#text_land {
  border: 1px solid #ccc;
  padding: 25px;
  margin-bottom: 30px;
}

textarea {
  width: 95%;
}

label {
  display: block;
  width: 50%;
  clear: both;
  margin: 0 0 .5em;
}

label select {
  width: 50%;
  float: right;
}

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  font-family: monospace;
  font-size: 1em;
}

h3 {
  margin: 1.2em 0;
}

div {
  margin: 1.2em;
}

textarea {
  width: 100%;
}

button {
  padding: .5em;
}

p {
  /*Here the sliles for OTHER paragraphs*/
}

#content p {
  font-size: inherit;
  /*So it gets the font size set on the #content div*/
  padding: 1.2em .5em;
  margin: 1.4em 0;
  border: 1px dashed #aaa;
  overflow: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <h3>Import Text below, then press the button</h3>
  <textarea id="textarea1" placeholder="Type text here, then press the button below." rows="5">
  </textarea>
  <input style="width:200px;" placeholder="Custom Characters per box">
  <button>
    Go
  </button>
  <br>

  <button style="width:200px;" id="go">Divide Text into Paragraphs</button>
</div>
<div>
  <h3 align="right">Divided Text Will Appear Below:</h3>
  <hr>
  <div id="content"></div>
</div>

詹森·约翰

id输入一个。

<input id="custom" placeholder="Custom Characters per box" style="width:200px;">

将以下代码添加到initialDistribute函数中。

custom = parseInt(document.getElementById("custom").value); //Get value of the input.

chunkSize = (custom>0)?custom:100; //If Custom value is more than `0`, take that as `chunkSize` value else `100`

见小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何通过 ref 获取 input(StyledComponent) 值

来自分类Dev

如何通过URL动态更改输入值

来自分类Dev

如何通过JS更改<ion-input> </ ion-input>元素的输入值

来自分类Dev

如何通过JS更改<ion-input> </ ion-input>元素的输入值

来自分类Dev

使用JQuery通过下拉列表动态更改PHP值

来自分类Dev

如何通过 Fl_Input 的值设置 Fl_Slider

来自分类Dev

如何防止更改预设的INPUT值?

来自分类Dev

如何使用jQuery通过custum属性动态更改html

来自分类Dev

如何动态更改xpath的值

来自分类Dev

如何使用 Javascript 动态更改“<input>”元素的样式?

来自分类Dev

JQuery:如何动态更改颜色?

来自分类Dev

如何获取<input>的值

来自分类Dev

通过Jquery更改按钮值

来自分类Dev

如何通过jquery更改backgroundcolor?

来自分类Dev

如何通过引用而不是通过@Input装饰器按值将父属性传递给子组件?

来自分类Dev

如何通过Java代码动态更改fontFamily

来自分类Dev

如何通过动态反应更改类的 css

来自分类Dev

如何知道@Input何时更改?

来自分类Dev

如何捕捉@Input值更改

来自分类Dev

通过<input>标签更新数组的值

来自分类Dev

如何动态更改线插值

来自分类Dev

如何使用vue动态更改所选值?

来自分类Dev

如何动态更改线插值

来自分类Dev

如何动态更改 <a-text> 的文本值?

来自分类Dev

如何动态更改下拉值

来自分类Dev

通过单击SELECT选项动态将text-INPUT元素的VALUE设置为默认值

来自分类Dev

通过单击SELECT选项将text-INPUT元素的VALUE动态设置为默认值

来自分类Dev

如何通过每行中的动态添加按钮更改HTML表格单元格的值

来自分类Dev

如何选择通过jquery动态添加的类?

Related 相关文章

热门标签

归档