[英]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 创建了一个示例结构。
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"), "&").replace(new RegExp("<", "g"), "<"); /* 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.