简体   繁体   English

如何将可关闭的文本标签添加到 Textarea Kendo | jQuery

[英]How to add closeable text tags to Textarea Kendo | jQuery

I need to use Text area like this image.我需要像这张图片一样使用文本区域。

在此处输入图像描述

I should able to click Text A , Text B , Text C , Text D buttons and, once I click any of this button it should add to the Text area and also able remove added text field from the Text area.我应该能够单击Text AText BText CText D按钮,一旦我单击任何此按钮,它应该添加到文本区域,还可以从文本区域中删除添加的文本字段。 Can I do it using jQuery UI , jQuery or JavaScript .我可以使用jQuery UIjQueryJavaScript Kendo UI is also okay. Kendo UI也不错。 but I'm unable to found my requirement support Kendo component to do this.但我无法找到我的要求支持 Kendo 组件来执行此操作。

I researched and found this http://skfox.com/jqExamples/insertAtCaret.html , but it's not support added text fields removable function,我研究并发现了这个http://skfox.com/jqExamples/insertAtCaret.html ,但它不支持添加文本字段可移动 ZC1C425268E68385D14AB5074C17A,

As was mentioned in my previous comments on your previous post, this cannot be done with a <textarea> element.正如我之前对您上一篇文章的评论中提到的,这不能通过<textarea>元素来完成。 These elements can only contain text, they cannot contain other elements like <button> or <span> which would be required to make a remove button.这些元素只能包含文本,不能包含其他元素,如<button><span> ,这些元素是制作删除按钮所必需的。

The following is a very lightweight example and it has many pitfalls.下面是一个非常轻量级的例子,它有很多陷阱。 It does give you some ideas of how you might look at proceeding.它确实为您提供了一些关于如何看待进行的想法。

 $(function() { function calcWordWidth(str, fontfamily, fontsize) { var word = $("<span>").css({ display: "none", "font-family": fontfamily, "font-size": fontsize }).html(str).appendTo("body"); var width = word.width(); word.remove(); return width; } function addCloseButton(pos, st, en, trg) { var btn = $("<span>", { class: "closeBtn" }).html("x"); btn.css({ position: "absolute", left: pos + "px", top: "1px" }); trg.parent().append(btn); btn.click(function() { removeText(st, en, trg); $(this).remove(); }); } function addText(str, trg) { var cur = trg.val(); var start = cur.length; if (start) { trg.val(cur + " " + str); } else { trg.val(str); } cur = trg.val(); var end = cur.length; var width = calcWordWidth(cur, trg.css("font-family"), trg.css("font-size")); console.log(width); addCloseButton(width, start, end, $("#txtMessage")); } function removeText(start, end, trg) { var cur = trg.val(); var upd = cur.slice(0, start) + " " + cur.slice(end); trg.val(upd); } $("button").click(function() { addText($(this).val(), $("#txtMessage")); }); });
 .closeBtn { font-family: Arial; font-size: 12px; cursor: pointer; padding: 1px; background: #ccc; border-radius: 3px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="maincontainer"> <div id="navtoplistline">&nbsp;</div> <div id="contentwrapper"> <div><button id="btn-1" value="Hello World:">Hello World;</button></div> <div id="maincolumn"> <div class="text" style="position: relative;"> <textarea name="txtMessage" id="txtMessage" class="txtDropTarget ui-droppable" cols="80" rows="15"></textarea> </div> </div> </div> </div>

You can also look at using a <div> element with the contenteditable attribute enabled.您还可以查看启用了contenteditable属性的<div>元素。 Again, pretty complex and would not advise it.同样,非常复杂,不建议这样做。

As I suggested, you may be better off using something like TinyMCE.正如我所建议的,您最好使用 TinyMCE 之类的东西。 TinyMCE is a JavaScript based Rich Text editor that is highly customizable. TinyMCE 是一个高度可定制的基于 JavaScript 的富文本编辑器。

Example: https://jsfiddle.net/Twisty/fngjcse3/示例: https://jsfiddle.net/Twisty/fngjcse3/

JavaScript JavaScript

tinymce.init({
  selector: 'textarea',
  menubar: false,
  statusbar: false,
  plugins: "code",
  toolbar: 'helloWorld allBase code',
  setup: function(editor) {
    var makeSpan = function(str) {
      return '<span class="word">&nbsp;' + str + '&nbsp;<em>x</em><span>&nbsp;';
    }
    editor.ui.registry.addButton('helloWorld', {
      text: 'Hello World!',
      onAction: function(_) {
        editor.insertContent(makeSpan("Hello World!"));
      }
    });
    editor.ui.registry.addButton('allBase', {
      text: 'All your Base',
      onAction: function(_) {
        editor.insertContent(makeSpan("All your base"));
      }
    });
  },
  content_style: 'span.word em { font-style: normal; font-size: 12px; background: #ccc; cursor: pointer; padding: 1px; border-radius: 3px; }',
  init_instance_callback: function(editor) {
    editor.on('click', function(e) {
      if (e.target.nodeName == "EM") {
        console.log("Remove Word.");
        e.target.parentElement.remove();
      }
    });
  }
});

This initializes TinyMCE with custom buttons.这将使用自定义按钮初始化 TinyMCE。 These buttons add the HTML that would be needed.这些按钮添加了需要的 HTML。 You can also initialize it with custom callbacks, this can handle the close or remove options you are looking for.您还可以使用自定义回调对其进行初始化,这可以处理您正在寻找的关闭或删除选项。

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

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