简体   繁体   English

添加文本 onclick 在 Textarea 中的文本输入/按键后停止工作 [已解决]

[英]Add Text onclick stop working after Text input / keypress in Textarea [solved]

i need some help.我需要一些帮助。

I programming an update manager for my CMS, this should get a form with script syntax highlight.我为我的 CMS 编写了一个更新管理器,这应该会得到一个带有脚本语法突出显示的表单。

I would like to have a few button with example/code lines, these should be placed in the input field onclick.我想要一些带有示例/代码行的按钮,这些应该放在输入字段 onclick 中。

The onclick function works as it should, but as soon as I typed in with keyboard, my buttons stopped working. onclick function 可以正常工作,但是一旦我用键盘输入,我的按钮就停止工作了。

There is no JS error message to work with.没有可使用的 JS 错误消息。

I need your help.我需要你的帮助。 I have created an example structure at codepen.我在 codepen 创建了一个示例结构。

My Codepen我的密码笔

Thx @ll谢谢@ll

 function update(text) { let result_element = document.querySelector("#__highlightingC"); // Handle final newlines (see article) if(text[text.length-1] == "\n") { text += " "; } // Update code result_element.innerHTML = text.replace(new RegExp("&", "g"), "&amp;").replace(new RegExp("<", "g"), "&lt;"); /* Global RegExp */ // Syntax Highlight Prism.highlightElement(result_element); } function sync_scroll(element) { /* Scroll result to scroll coords of event - sync with textarea */ let result_element = document.querySelector("#__highlighting"); // Get and set x and y result_element.scrollTop = element.scrollTop; result_element.scrollLeft = element.scrollLeft; } function check_tab(element, event) { let code = element.value; if(event.key == "Tab") { /* Tab key pressed */ event.preventDefault(); // stop normal let before_tab = code.slice(0, element.selectionStart); // text before tab let after_tab = code.slice(element.selectionEnd, element.value.length); // text after tab let cursor_pos = element.selectionEnd + 1; // where cursor moves after tab - moving forward by 1 char to after tab element.value = before_tab + "\t" + after_tab; // add tab char // move cursor element.selectionStart = cursor_pos; element.selectionEnd = cursor_pos; update(element.value); // Update text to include indent } } function cmsSqlAddRule(toId, rule) { var getOldVar = '', id; id = document.getElementById(toId); rule = rule.replace(/<(.*?)>/gi,''); id.innerHTML += ''+rule+'\n'; //id.focus(); update(id.value); sync_scroll(id); }
 <script src='https://mazzimo24.net/includes/js/inlineEditorHighlight/prism.js'></script> <link rel='stylesheet' href='https://mazzimo24.net/includes/js/inlineEditorHighlight/style.css'> <script id='mazzimoFile2' src='https://mazzimo24.net/admin/js/sqlHighlight.js'></script> <div style="height: 500px"> <textarea placeholder = "// Enter Update-Code" id="__editing" spellcheck="false" oninput = "update(this.value); sync_scroll(this);" onscroll = "sync_scroll(this);" onkeydown = "check_tab(this, event); update(this.value);" ></textarea> <pre id="__highlighting" aria-hidden="true"> <code class="language-php" id="__highlightingC"></code> </pre> <div style='clear: both; width: 100%; margin-bottom: 0px;'></div> </div> <input type='button' class='button' name='generate_update' value='Add' style='width: 75px; white-space: nowrap; line-height: 1.0rem;important,' onclick="cmsSqlAddRule('__editing'. '$sql = \' ALTER TABLE \'.$db_prefix?\'?<b>table1</b>? ADD?column; varchar(255) \';'):" /> <input type='button' class='button' name='generate_update' value='Add2' style='width; 75px: white-space; nowrap: line-height. 1;0rem,important.' onclick="cmsSqlAddRule('__editing'. '$sql = \' ALTER TABLE \'?$db_prefix?\'?<b>table2</b>? ADD;column; varchar(255) \';');" />

id.innerHTML to id.value was the solution. id.innerHTML 到 id.value 是解决方案。

function cmsSqlAddRule(toId, rule) {
    var getOldVar = '', id;
        id = document.getElementById(toId);
        rule = rule.replace(/<(.*?)>/gi,'');
        id.value += ''+rule+'\n';
        //id.focus();
        update(id.value); 
        sync_scroll(id);
}

停止在其中添加文本<textarea>&lt;i&gt;after condition fulfill&lt;/div&gt;&lt;/i&gt;&lt;b&gt;条件满足后&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我有一个文本区域</p><pre>&lt;textarea rows="3" [(ngModel)]="formatObj.textAreaBox" (keyup)="onExtendexDelimeterKeyUp($event)" #extendDelimiters&gt;&lt;/textarea&gt;</pre><p> 我想在条件满足后停止在文本区域中添加文本。 我不想禁用 textarea,因为用户可以删除 textarea 中的文本。 我想停止使用 Angular 7 执行 keyup</p><p> 谢谢</p></div> - Stop adding text in <textarea> after condition fulfill

暂无
暂无

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

相关问题 按键文本未传递到文本区域元素 - Keypress text is not passed to the textarea element 停止在其中添加文本<textarea>&lt;i&gt;after condition fulfill&lt;/div&gt;&lt;/i&gt;&lt;b&gt;条件满足后&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我有一个文本区域</p><pre>&lt;textarea rows="3" [(ngModel)]="formatObj.textAreaBox" (keyup)="onExtendexDelimeterKeyUp($event)" #extendDelimiters&gt;&lt;/textarea&gt;</pre><p> 我想在条件满足后停止在文本区域中添加文本。 我不想禁用 textarea,因为用户可以删除 textarea 中的文本。 我想停止使用 Angular 7 执行 keyup</p><p> 谢谢</p></div> - Stop adding text in <textarea> after condition fulfill Javascript按钮单击触发按键并在输入文本中添加文本 - Javascript button click trigger keypress and add text into input text 在工具栏中选择文本后的HTML工具提示 <input> 或在 <textarea> - HTML Tooltip after selecting text in an <input> or in a <textarea> 在textarea中的文本后添加白色断线 - Add white breaking line after text in textarea 文本编辑后,追加功能不适用于textarea - append function not working to textarea after text edited 模拟被按下的键盘上的按钮,将该键的值添加到文本输入中 - Simulate a button on the keyboard being pressed, add the value of that keypress to text input onclick选择全文本textarea - onclick select whole text textarea Javascript到Jquery,在输入onclick中添加文本 - Javascript to Jquery, add text in input onclick 当用户停止选择输入或文本区域的文本时,Javascript 中是否存在事件? - Exist an event in Javascript when the user stop select the text of a input or textarea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM