我正在开发一个网页,让用户能够写下自己在学校项目中的笔记。我的想法是让他们使用按钮来加粗/斜体/下划线他们的文本。目前,这些按钮可以使用,但会将文本区域内所有的内容都加粗/斜体/下划线。我希望它能够按照他们所选择的文本进行加粗/斜体/下划线。
另外,我希望知道如何让用户点击加粗按钮后,他们输入的文本从那时起变成加粗字体。再次点击该按钮,则输入的文本恢复正常字体。
<script type="text/javascript">
function boldText(){
var target = document.getElementById("TextArea");
if( target.style.fontWeight == "bolder" ) {
target.style.fontWeight = "normal";
} else {
target.style.fontWeight = "bolder";
}
}
function italicText(){
var target = document.getElementById("TextArea");
if( target.style.fontStyle == "italic" ) {
target.style.fontStyle = "normal";
} else {
target.style.fontStyle = "italic";
}
}
function underlineText(){
var target = document.getElementById("TextArea");
if( target.style.textDecoration == "underline" ) {
target.style.textDecoration = "none";
} else {
target.style.textDecoration = "underline";
}
}
</script>
contenteditable
, 然后添加样式。 - Harry Bomrah