简体   繁体   English

如何将突出显示的文本从textarea提交到数据库

[英]how to submit highlighted text from textarea into database

I have to save highlighted text from textarea into database on click event how can i do it. 我必须在点击事件中将突出显示的文本从textarea保存到数据库中,我该怎么做。 I found some code from so but it doesn't work for me. 我从中找到了一些代码,但对我不起作用。

 $('#send').on("click", function(){ ShowSelection(); }); function ShowSelection() { var textComponent = $('#my-content span').val(); console.log(textComponent); var selectedText; if (textComponent.selectionStart !== undefined) {// Standards Compliant Version var startPos = textComponent.selectionStart; var endPos = textComponent.selectionEnd; selectedText = textComponent.value.substring(startPos, endPos); } else if (document.selection !== undefined) {// IE Version textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; } alert("You selected: " + selectedText); } 

There is a lot wrong with your code: 您的代码有很多错误:

selectionStart and selectionEnd are properties of form elements (input, textarea), but $('#my-content span') is obviously finding a <span> element. selectionStartselectionEnd是表单元素(输入,文本区域)的属性,但是$('#my-content span')显然在查找<span>元素。

$('#my-content span').val() will return a string (in the case of an input it's the value of that input, but in your case it will be empty, because you apply it on a span element. $('#my-content span').val()将返回一个字符串(在输入的情况下,它是该输入的值,但在您的情况下,它将为空,因为您将其应用于span元素。

textComponent.selectionStart : Since textComponent is a string and not an HTML element there is no property selectionStart on textComponent . textComponent.selectionStart :由于textComponent是字符串而不是HTML元素,因此textComponent上没有属性selectionStart

-- -

Working example for a <textarea> element ( see here ): <textarea>元素的工作示例( 请参见此处 ):

    var startPos = $('textarea')[0].selectionStart
    // etc.

Working example for <span> element ( see here ): <span>元素的工作示例( 请参见此处 ):

    var selectedText = window.getSelection().toString()

如何访问当前突出显示的文本<textarea>&lt;i&gt;in Javascript&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在Javascript中&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 用户将在页面上的 HTML 文本区域中输入文本。 当他们突出显示该文本的一部分时,我如何访问突出显示的字符串? 突出显示 textarea 元素中的文本是否会触发任何事件?</p><p> 这是在文本区域是一个组件的反应应用程序的上下文中完成的。</p></div> - How to access the currently highlighted text within a <textarea> in Javascript

暂无
暂无

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

相关问题 如何从textarea获取突出显示的文本位置? - How to get highlighted text position from textarea? 如何从文本区域中突出显示的文本中获取邻居字符? - How to get neighbor character from highlighted text in the textarea? 在文本区域中显示突出显示的文本 - Displaying highlighted text in textarea 如何在另一个文本区域中显示文本区域和可编辑 div 中突出显示的文本的 position? - How to show the position of highlighted text in a textarea and editable div, in a another textarea? 如何将接收到的文本从数据库嵌入到文本区域 - How to embed a received text from database to textarea 从div获取突出显示的文本并复制到textarea javascript中 - Get highlighted text from div and copy into textarea javascript 从Textarea添加部分文本以某种方式着色/突出显示 - Adding part of the text from Textarea colored/highlighted somehow 如何访问当前突出显示的文本<textarea>&lt;i&gt;in Javascript&lt;/div&gt;&lt;/i&gt;&lt;b&gt;在Javascript中&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 用户将在页面上的 HTML 文本区域中输入文本。 当他们突出显示该文本的一部分时,我如何访问突出显示的字符串? 突出显示 textarea 元素中的文本是否会触发任何事件?</p><p> 这是在文本区域是一个组件的反应应用程序的上下文中完成的。</p></div> - How to access the currently highlighted text within a <textarea> in Javascript 如何在 textarea(坐标)angular 中获取高亮显示的文本? - How to get postition highlighted text in textarea (coords) angular? 在反应 js 中突出显示文本的 textarea 组件 - textarea component with highlighted text in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM