简体   繁体   English

在输入中按下输入时如何在标签上插入新行

[英]How to insert new line on label when pressed enter in input

I am dynamically changing the value of a text (destinationtext) using an input with javascript.我正在使用带有 javascript 的输入动态更改文本(目标文本)的值。 It's a multiline input.这是一个多行输入。 But i can't add a new line at destination text when I press enter.但是当我按 Enter 时,我无法在目标文本中添加新行。 How can i do this?我怎样才能做到这一点?

   <div id="canvas1">
            <img src="~/img/temp_promosyon+fiyat_mobil.jpg" class="background1" />
            <img class="picturecanvas1mob" id="pic1mob" src="~/img/karisik_sandvic-mobil.jpg">
            <div class="logocanvas1mob"><span></span></div>
            <div class="destinationtext"><span id="destinationtext">@Model.PromosyonMetin</span></div>
            <div class="promfiyat1mob"><span id="fiyatmobdest">@Model.Fiyat</span></div>
        </div>

.... inside the form this is the input i use 

<div class="form-group row">
                <label asp-for="PromosyonMetin" class="col-sm-2 col-form-label">Metin</label>
                <div class="col-sm-10">
                    <textarea asp-for="PromosyonMetin" type="text" class="form-control" id="sourceinput" placeholder="Bol kabaklı mücver menüsü..." onKeyPress="copyText()"></textarea>
                </div>
            </div>

....

This is the div's style这是div的样式

  .destinationtext {
        position: absolute;
        top: 0px;
        left: 289px;
        width: 233px;
        height: 122px;
        line-height: 122px;
        z-index: 13;
        text-align: center;
        font-family: Hind;
        font-size: 16px;
        vertical-align: middle;
        color: white;
        margin: 5px 5px 5px 5px;
        padding-left: 10px;
        padding-right: 10px;
    }

I'm using this simple code to change values我正在使用这个简单的代码来更改值

  $(function () {
        $('#sourceinput').keyup(function () {
            $('#destinationtext').text($(this).val());
        });
    });

My problem basically looks like this:我的问题基本上是这样的:

我的画布和文本输入

I think you just need to apply a style to the div我认为您只需要将样式应用于div

#destinationtext {
   white-space: pre-wrap;
}

If you want to see line breaks in your span element, you need to change the newline character from the input into a <br> tag.如果您想在span元素中看到换行符,则需要将输入中的换行符更改为<br>标记。 Something like:就像是:

$('#sourceinput').keyup(function () {
    $('#destinationtext').html($(this).val().replace(/\n/g, '<br>'));
});

Note that I'm using .html() instead of .text() because .text() would encode HTML tags.请注意,我使用.html()而不是.text()因为.text()会编码 HTML 标签。 The main difference is that .html() will replace any other HTML content that you might have in the span.主要区别在于.html()将替换您在 span 中可能拥有的任何其他 HTML 内容。

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

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