简体   繁体   English

如何通过此文本框将多行添加到不同的不同 div/span 标签中?

[英]How can i add multi lines together into different different div / span tag through this text box?

How can i add multi lines together into different different span tag through this text box ?如何通过此文本框将多行添加到不同的不同跨度标签中?

There is an text box, by using this box i can insert a new div / span class content Right ?有一个文本框,通过使用这个框,我可以插入一个新的 div/span 类内容,对吗? But every time when i need to add new class i need to write a new line in this text box and need to press send button, Now i want that if i have 10 lines content together with "Enter" button line break但是每次当我需要添加新类时,我都需要在这个文本框中写一个新行并需要按下发送按钮,现在我想要的是,如果我有 10 行内容和“Enter”按钮换行符

Like喜欢
My Line 1 is here我的 1 号线在这里
My Line 2 is here我的 2 号线在这里
My Line 3 is here我的 3 号线在这里
My Line 4 is here我的 4 号线在这里
My Line 5 is here我的 5 号线在这里
My Line 6 is here我的 6 号线在这里
My Line 7 is here我的 7 号线在这里
My Line 8 is here我的 8 号线在这里
... and so on ... 等等

then i want to paste all 10 lines in this text box together and by pressing send button i want that all lines must be add in different different div / span class not all in one class with < br > tag that is working now.然后我想将这个文本框中的所有 10 行粘贴在一起,并通过按发送按钮我希望所有行都必须添加到不同的不同 div/span 类中,而不是全部在一个带有 <br> 标签的类中,现在正在工作。

so plz help me to improve my code Love you Helper and Thanks in Advance所以请帮我改进我的代码爱你帮手,提前致谢

 const sendButton = document.getElementById('send-btn'); const textArea = document.getElementById('input'); const innerDiv = document.getElementById('inner'); var message = textArea.value; sendButton.addEventListener('click', function () { const message = new MessageContainerBuilder().BuildMessage(textArea.value); innerDiv.appendChild(message); textArea.value = ''; }); function encodeHtmlEntity(input) { var output = input.replace(/[\ -\香<>\\&]/gim, function (i) { return '&#' + i.charCodeAt(0) + ';'; }); return output; } function MessageContainerBuilder() { var createDivElement = function (classTest) { var div = document.createElement('div'); var classAttr = document.createAttribute('class'); classAttr.value = classTest; div.setAttributeNode(classAttr); return div; }; var createSpanElement = function (value, classTest) { var span = document.createElement('span'); if (classTest) { var classAttr = document.createAttribute('class'); classAttr.value = classTest; span.setAttributeNode(classAttr); } span.innerText = encodeHtmlEntity(value); return span; }; this.BuildMessage = function (text) { var divContainer = createDivElement('outgoing'); var messageSpan = createSpanElement(text, 'me'); divContainer.appendChild(messageSpan); return divContainer; }; }
 <div id="inner"> <div class="incoming"> <div class="them">Lorem </div> </div> <div class="outgoing"> <div class="me">Lorem ipsum </div> </div> </div> <textarea class="input" id="input" placeholder="Message..."></textarea> <button class="waves-effect waves-light" id="send-btn">Send</button>

so plz help me to improve my code Love you Helper and Thanks in Advance所以请帮我改进我的代码爱你帮手,提前致谢

You can try using String.prototype.split() so that each text is separated by \\n (new line) into an array, then you can iterate over each element like using Array.prototype.forEach() .您可以尝试使用String.prototype.split()以便每个文本由\\n (新行)分隔成一个数组,然后您可以像使用Array.prototype.forEach()一样迭代每个元素。 Here's the code you can use:这是您可以使用的代码:

sendButton.addEventListener('click', function () {
  textArea.value.split('\n').forEach((text) => {
    const message = new MessageContainerBuilder().BuildMessage(text);
    innerDiv.appendChild(message);
  });
  textArea.value = '';
});

I took some liberties and simplified your code as you can see below.我采取了一些自由并简化了您的代码,如下所示。 It does everything I believe your code was attempting.它完成了我认为您的代码正在尝试的一切。

Note that I'm using .split("\\n") to break your input based on each newline character and then iterate over that as necessary.请注意,我使用.split("\\n")根据每个换行符来中断您的输入,然后根据需要对其进行迭代。

Also, you said you were inserting a div/span, but I don't see your code actually creating a span tag.另外,你说你要插入一个 div/span,但我没有看到你的代码实际上创建了一个 span 标签。 I wrote my code to do that for you though.我写了我的代码来为你做这件事。

 const sendButton = document.getElementById('send-btn'); const textArea = document.getElementById('input'); const innerDiv = document.getElementById('inner'); var message = textArea.value; sendButton.addEventListener('click', function() { // split the textarea entries into an array let lines = (textArea.value).split("\\n"); // iterate over each line, creating a div/span and inserting into the DOM lines.forEach( (line) => { let encodedLine = encodeHtmlEntity(line); let newElement = `<div class="me"><span>${encodedLine}</span></div>`; innerDiv.innerHTML += newElement; }); // reset the textarea textArea.value = ''; }); function encodeHtmlEntity(input) { var output = input.replace(/[\ -\香<>\\&]/gim, function(i) { return '&#' + i.charCodeAt(0) + ';'; }); return output; }
 <div id="inner"> <div class="incoming"> <div class="them">Lorem </div> </div> <div class="outgoing"> <div class="me">Lorem ipsum </div> </div> </div> <textarea class="input" id="input" placeholder="Message..."></textarea> <button class="waves-effect waves-light" id="send-btn">Send</button>

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

相关问题 在单独的div标签中单击时,如何使每个标签显示不同的文本块? - How can I make each a tag display a different chunk of text when clicked in a separate div tag? 如何遍历div并找到最近的跨度并为每个跨度添加不同的东西? - How do I loop through divs and find the nearest span and add different things to each span? 如何在搜索框中向文本添加标签 - How can i add tag to text in search box 如何用搜索到的文本替换不同的div标签? - How to replace a different div tag with the searched text? 如何切换文本框<label>,</label><div> <label>,或<span>使用 jQuery?</span></label> - How can I switch a text box for a <label>, <div>, or <span> using jQuery? 如何添加多个元素,例如<span>,</span> <p> <span>要么</span> <div> <span>内</span> <option> <span>的标签</span> <select> <span>在ReactJs中?</span> - How can I add multiple elements like <span>, <p> or <div> inside <option> tag of <select> in ReactJs? 如何添加彼此不同的div中的2个按钮? - How can I add 2 buttons that are in different div to each other? 如何在div中添加标签 - How can I Add a tag in a div 如何<span>在JQuery</span>中将<span>标签</span>添加<span>到所选文本</span> - How to add <span> tag to a selected text in JQuery 如何在同一个DIV标签中添加2个不同的onclick函数 - How to add 2 different onclick functions to the same DIV tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM