问题
I am currently working on a project for school and am required to create a blog. Part of our assignment is to create an input field that would allow users to customize the styling of their text within the field. Although I have looked around, I have had no luck in finding what it is that I am looking for. For clarification, the goal is to create a field that resembles the field on here when creating a post (Photo included below).
I am assuming that Javascript and/or JQuery is required, although I am unsure where to begin. Features would include text styling, addition of links, keeping track of [return]s (to create new paragraphs) and any other HTML/CSS elements.
Below, I will include examples of what it is exactly that I am wanting in case I am in any way unclear.
Thank you in advance for any help/guidance!
Example 1:

Example 2:

回答1:
You could create it yourself!
I threw this together in a couple of minutes. It's pretty rudimentary, but if you want to build it yourself, this could be a starting point for you:
var areas = document.getElementsByClassName('augmented-textarea');
var commands = [{
display: "<b>B</b>",
f: function(text) {
return "**" + text + "**";
},
}, {
display: "<i>I</i>",
f: function(text) {
return "_" + text + "_";
},
}, ];
Array.prototype.forEach.call(areas, initTextarea);
function initTextarea(t) {
var wrapper = document.createElement('div');
wrapper.classList.add('textarea-wrapper');
var bar = document.createElement('div');
bar.classList.add('textarea-bar');
commands.forEach(function(c) {
var b = document.createElement('button');
b.type = 'button';
b.innerHTML = c.display;
b.addEventListener('click', function() {
if (t.selectionStart === t.selectionEnd)
return;
var v = t.value,
ss = t.selectionStart,
se = t.selectionEnd,
prefix = v.slice(0, ss),
suffix = v.slice(se),
target = v.substring(ss, se),
changed = c.f(target);
t.value = prefix + changed + suffix;
t.selectionStart = ss;
t.selectionEnd = ss + changed.length;
t.focus();
});
bar.appendChild(b);
});
t.parentNode.insertBefore(wrapper, t);
t.parentNode.removeChild(t);
wrapper.appendChild(bar);
wrapper.appendChild(t);
}
.textarea-wrapper {
display: flex;
overflow: hidden;
width: 300px;
flex-direction: column;
border: 1px solid gray;
}
.textarea-bar {
background-color: #ddd;
min-height: 30px;
line-height: 30px;
}
.textarea-bar button {
background-color: #ddd;
font-family: serif;
font-size: 20px;
width: 30px;
display: inline-block;
border: 1px solid gray;
border-radius: 3px;
margin: 3px;
cursor: pointer;
}
.augmented-textarea {
width: 100%;
max-width: 100%;
height: 70px;
box-sizing: border-box;
border: 0;
}
<textarea class="augmented-textarea">Select some text and press one of the buttons above!</textarea>
Each command is registered in the commands
array, with HTML for a button, and an instruction to do with the selected text.
回答2:
Use a text editor for this. There are lot of text editor plugins are available.
This may help you : http://ckeditor.com/
Note: Have a look in to the Licence also before using the editor.
回答3:
Just use tinymce - unless your assignment is to create the actual control itself, which is far more complicated than even creating a blog unfortunately.
Get TinyMCE here: https://github.com/tinymce
or at their site: https://www.tinymce.com/
Then create a textarea element and initiate the control:
Include TinyMCE at the bottom of your page and include this script below that include.
Example here @ the TinyMCE site: https://www.tinymce.com/docs/demo/basic-example/#liveexample
HTML:
<textarea></textarea>
Javascript:
tinymce.init({
selector: 'textarea',
height: 500,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen',
'insertdatetime media table contextmenu paste code'
],
toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
content_css: [
'//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
'//www.tinymce.com/css/codepen.min.css'
]
});
来源:https://stackoverflow.com/questions/35953345/how-to-allow-users-to-customize-text-field-with-styling