简体   繁体   English

如何在所选文本的textarea中转换文本大小写?

[英]How to convert text case in textarea of selected text?

I cannot make this script work and I'm stuck with this for days now.我无法让这个脚本工作,而且我已经坚持了好几天了。 It's supposed to change the text case only of the selected words (highlighted by cursor) and not all of the texts at the same time.它应该更改所选单词(由光标突出显示)的文本大小写而不是同时更改所有文本。

It's really because I don't know how to properly use the selection line that I saw from somewhere.这真的是因为我不知道如何正确使用我从某个地方看到的选择线。 Any other tutorial I see online will either have only 1 of the 4 functions I need or they will have it all but will convert all text and not the selected only.我在网上看到的任何其他教程要么只有我需要的 4 个功能中的 1 个,要么他们拥有所有功能,但会转换所有文本,而不是仅转换选定的文本。

My current script looks like this:我当前的脚本如下所示:

 var stringbox = document.getElementById('textarea2') $(document).ready(function () { var selection; $(textarea2).select(function () { selection = window.getSelection().toString(); }); function convertstring(textarea, action){ if (action == 'sentencecase'){ textarea.value = sentenceCase(textarea.value) } else if (action == 'titlecase'){ textarea.value = toTitleCase(textarea.value) } else if (action == 'capitalcase'){ textarea.value = CapitalCase(textarea.value) } else if (action == 'lowercase'){ textarea.value = lowerCase(textarea.value) } else if (action == 'uppercase'){ textarea.value = upperCase(textarea.value) } return false } function sentenceCase(str){ var str = str.toLowerCase().replace(/\\si\\s/g, ' I '); str = str.charAt(0).toUpperCase() + str.slice(1); return str } function toTitleCase(str){ var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\\.?|via)$/i; var str = str.toLowerCase() return str.replace(/[A-Za-z0-9\À-\ÿ]+[^\\s-]*/g, function(match, index, title){ if (index > 0 && index + match.length !== title.length && match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" && (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') && title.charAt(index - 1).search(/[^\\s-]/) < 0) { return match.toLowerCase(); } if (match.substr(1).search(/[AZ]|\\../) > -1) { return match; } return match.charAt(0).toUpperCase() + match.substr(1); }); }; function CapitalCase(str){ return str.toLowerCase().split(' ').map(function(word) { return (word.charAt(0).toUpperCase() + word.slice(1)); }).join(' '); } function lowerCase(str){ return str.toLowerCase(); } function upperCase(str){ return str.toUpperCase(); }
 <textarea id="textarea2" spellcheck="true" placeholder="Use as your notepad.."></textarea> <button title="Convert to Title Case" class="sbtn" onclick="return convertstring(stringbox, 'titlecase')">Title Case</button> <button title="Convert to Sentence Case" class="sbtn" onclick="return convertstring(stringbox, 'sentencecase')">Sentence Case</button> <button title="Convert to Upper Case" class="sbtn" onclick="return convertstring(stringbox, 'uppercase')">Upper Case</button> <button title="Convert to Lower Case" class="sbtn" onclick="return convertstring(stringbox, 'lowercase')" string.="" the="">Lower Case</button>

I'm not good with javascript, so please excuse the code below :(我不擅长 javascript,所以请原谅下面的代码:(

Best way for these operations is to use " execCommand() "这些操作的最佳方法是使用“ execCommand()

here I have used execCommand("insertText", false, <textToReplaceWith>) to simply replace selection with desired text.在这里,我使用execCommand("insertText", false, <textToReplaceWith>)简单地用所需的文本替换选择。

I have commented unwanted code as it was resulting in errors我已经评论了不需要的代码,因为它会导致错误

I presume this would fix the issue.我想这会解决这个问题。

 var stringbox = document.getElementById('textarea2') //$(document).ready(function () { // var selection; //$(textarea2).select(function () { // selection = window.getSelection().toString(); //}); function convertstring(textarea, action){ var selectedText = ''; // window.getSelection if (window.getSelection) { selectedText = window.getSelection(); } // document.getSelection else if (document.getSelection) { selectedText = document.getSelection(); } // document.selection else if (document.selection) { selectedText = document.selection.createRange().text; } else return; if (action == 'sentencecase'){ alteredText = sentenceCase(selectedText.toString()) document.execCommand('insertText', false, alteredText) } else if (action == 'titlecase'){ alteredText = toTitleCase(selectedText.toString()) document.execCommand('insertText', false, alteredText) } else if (action == 'capitalcase'){ alteredText = CapitalCase(selectedText.toString()) document.execCommand('insertText', false, alteredText) } else if (action == 'lowercase'){ alteredText = lowerCase(selectedText.toString()) document.execCommand('insertText', false, alteredText) } else if (action == 'uppercase'){ alteredText = upperCase(selectedText.toString()) document.execCommand('insertText', false, alteredText) } return false } function sentenceCase(str){ var str = str.toLowerCase().replace(/\\si\\s/g, ' I '); str = str.charAt(0).toUpperCase() + str.slice(1); return str } function toTitleCase(str){ var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\\.?|via)$/i; var str = str.toLowerCase() return str.replace(/[A-Za-z0-9\À-\ÿ]+[^\\s-]*/g, function(match, index, title){ if (index > 0 && index + match.length !== title.length && match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" && (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') && title.charAt(index - 1).search(/[^\\s-]/) < 0) { return match.toLowerCase(); } if (match.substr(1).search(/[AZ]|\\../) > -1) { return match; } return match.charAt(0).toUpperCase() + match.substr(1); }); }; function CapitalCase(str){ return str.toLowerCase().split(' ').map(function(word) { return (word.charAt(0).toUpperCase() + word.slice(1)); }).join(' '); } function lowerCase(str){ return str.toLowerCase(); } function upperCase(str){ return str.toUpperCase(); }
 <textarea id="textarea2" spellcheck="true" placeholder="Use as your notepad.."></textarea> <button title="Convert to Title Case" class="sbtn" onclick="convertstring(stringbox, 'titlecase')">Title Case</button> <button title="Convert to Sentence Case" class="sbtn" onclick="convertstring(stringbox, 'sentencecase')">Sentence Case</button> <button title="Convert to Upper Case" class="sbtn" onclick="convertstring(stringbox, 'uppercase')">Upper Case</button> <button title="Convert to Lower Case" class="sbtn" onclick="convertstring(stringbox, 'lowercase')" string.="" the="">Lower Case</button>

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

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