繁体   English   中英

在Textarea中输入文本时,如何在div中显示文本

[英]How to show text in div, when text enter in Textarea

当在Textarea中输入文本时,在DIV中显示文本。但条件是,我追加了多个DIV,然后在Textare中键入文本。然后仅显示一个DIV,而未显示其他DIV。

我的代码:

<button data-bind="adds">ADD</button>
     <div data-bind="foreach: items">
                <div class="SpeechBubble" id="speechID" data-bind="attr: { class: 'SpeechBubble' }">
                <div class="pointer bottom " id="pointer"></div>
                <div class="txtspeech"></div>
            </div>

        </div>

var SpeechBubble = function () {
                this.items = ko.observableArray();
                this.adds = function (item) {
                    this.items.push(item);
                }
            }
            ko.applyBindings(new SpeechBubble());



$('.speechttxt').keyup(function () {
    var txt = $(this).val();
    $('.txtspeech').html(txt);
});

您的代码正在运行,可能需要将其放入document.ready,以便在执行绑定事件的脚本时,将您要查找的元素添加到DOM包含jQuery

现场演示

$('.speechttxt').keyup(function () {
    var txt = $(this).val();
    $('.txtspeech').html(txt);
});

您可以使用脚本标签添加jQuery。

<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>    

<script  type="text/javascript">
    document.ready(function(){
        $('.speechttxt').keyup(function () {
            var txt = $(this).val();
            $('.txtspeech').html(txt);
        });
    });
</script>

将类“ txtspeech”添加到要显示文本的div中

暂无
暂无

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

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