我在基于 Vuejs 的 Web 應(yīng)用程序中的文本區(qū)域中使用版本 5.65.7 的 CodeMirror 插件,一切正常,沒(méi)有任何問(wèn)題。我想將占位符添加到我的文本區(qū)域,因此我已將相應(yīng)的占位符文件添加到我的應(yīng)用程序中,并且可以在我的文本區(qū)域中看到占位符。
我想更改占位符的字體顏色并將其居中對(duì)齊,因此我嘗試對(duì) codemirror 樣式進(jìn)行一些修改,但由于某種原因它根本不起作用。您能告訴我如何更改 CodeMirror 控制的文本區(qū)域的字體顏色和居中占位符嗎?
我在這里查看了一個(gè)類(lèi)似的問(wèn)題:占位符字體顏色”并嘗試執(zhí)行相同的操作,但由于某種原因它不起作用。
我根據(jù)我的實(shí)際應(yīng)用程序創(chuàng)建了一個(gè)示例項(xiàng)目來(lái)演示 CodeSandBox 中的問(wèn)題。
我嘗試查看開(kāi)發(fā)工具并嘗試過(guò),但它沒(méi)有按預(yù)期工作。有人可以讓我知道我做錯(cuò)了什么并提供一些解決方法嗎?
以下是 CodeSandBox 中也提供的代碼示例:
<template> <div class="container"> <div class="row"> <div class="col-md-5 offset-md-1 mt-5 mr-2 mb-2 pt-2"> <textarea id="jsonEvents" ref="jsonEvents" v-model="jsonEvents" class="form-control" placeholder="Document in JSON format" spellcheck="false" data-gramm="false" /> </div> <div class="col-md-5 offset-md-1 mt-5 mr-2 mb-2 pt-2"> <textarea id="xmlEvents" ref="xmlEvents" v-model="xmlEvents" class="form-control" placeholder="Document in XML format" spellcheck="false" data-gramm="false" /> </div> </div> </div> </template> <script> let CodeMirror = null; if (typeof window !== "undefined" && typeof window.navigator !== "undefined") { CodeMirror = require("codemirror"); require("codemirror/mode/xml/xml.js"); require("codemirror/mode/javascript/javascript.js"); require("codemirror/lib/codemirror.css"); require("codemirror/addon/lint/lint.js"); require("codemirror/addon/lint/lint.css"); require("codemirror/addon/lint/javascript-lint.js"); require("codemirror/addon/hint/javascript-hint.js"); require("codemirror/addon/display/placeholder.js"); } export default { name: "Converter", components: {}, data() { return { xmlEvents: "", jsonEvents: "", xmlEditor: null, jsonEditor: null, }; }, mounted() { // Make the XML textarea CodeMirror this.xmlEditor = CodeMirror.fromTextArea(this.$refs.xmlEvents, { mode: "application/xml", beautify: { initialBeautify: true, autoBeautify: true }, lineNumbers: true, indentWithTabs: true, autofocus: true, tabSize: 2, gutters: ["CodeMirror-lint-markers"], lint: true, autoCloseBrackets: true, autoCloseTags: true, styleActiveLine: true, styleActiveSelected: true, }); // Set the height for the XML CodeMirror this.xmlEditor.setSize(null, "75vh"); // Make the JSON textarea CodeMirror this.jsonEditor = CodeMirror.fromTextArea(this.$refs.jsonEvents, { mode: "applicaton/ld+json", beautify: { initialBeautify: true, autoBeautify: true }, lineNumbers: true, indentWithTabs: true, autofocus: true, tabSize: 2, gutters: ["CodeMirror-lint-markers"], autoCloseBrackets: true, autoCloseTags: true, styleActiveLine: true, styleActiveSelected: true, }); // Set the height for the JSON CodeMirror this.jsonEditor.setSize(null, "75vh"); // Add the border for all the CodeMirror textarea for (const s of document.getElementsByClassName("CodeMirror")) { s.style.border = "1px solid black"; } }, }; </script> <style> textarea { height: 75vh; white-space: nowrap; resize: both; border: 1px solid black; } .cm-editor .cm-placeholder { color: red !important; text-align: center; line-height: 200px; } .CodeMirror-editor pre.CodeMirror-placeholder { color: red !important; text-align: center; line-height: 200px; } .CodeMirror-editor .CodeMirror-placeholder { color: red !important; text-align: center; line-height: 200px; } </style>
不知何故,我無(wú)法在不登錄的情況下使用您的codesandbox, 但您可以嘗試使用偽類(lèi),如下所示:
textarea::placeholder { color: red; }
請(qǐng)參閱此文檔。
如果可能,可以使用 Javascript 來(lái)實(shí)現(xiàn)此目的 -
let placeholder_el = document.querySelectorAll('pre.CodeMirror-placeholder')[0]; placeholder_el.style['color'] = 'red'; placeholder_el.style['text-align'] = 'center'; placeholder_el.style['line-height'] = '200px';