Contenteditable div 加粗选项

Contenteditable div with bold option

我想使用 粗体斜体 选项使内容可编辑 div 以在另一个 div 中显示内容在具有相同选项的键盘上。 我设法显示文本,但没有显示选项。请帮助

html:

<button onclick="document.execCommand('bold');">B</button>
<button onclick="document.execCommand('italic');">I</button>
<div id="textarea" contenteditable></div>
<div id="textarea-show"></div>

jquery:

$('#textarea').keyup(function() {
  $('#textarea-show').html($(this).text());
});

css:

#textarea { background-color: #fff;
  border: 1px solid #ccc;
  color: #555;
  font-size: 14px;
  height: 34px;
  width: 450px;
}

  #textarea-show{font-size: 2rem;
  color:#666;
  height:50px;
  border: 1px solid #ccc;
  width: 450px;
}

示例:https://jsfiddle.net/gqmLtct7/1/

您必须使用 CSS font-stylefont-weight 将您的 output text 转换为 粗体 斜体如下,

$('#textarea').keyup(function() {
  $('#textarea-show').html($(this).text());
});
$(".bld").on('click',function(){
 var a = $('#textarea-show').html($("#textarea").text());
 $(a).css('font-weight','bold');
  $(a).css('font-style','normal');
});

$(".itl").on('click',function(){
 var a = $('#textarea-show').html($("#textarea").text());
 $(a).css('font-style','italic');
 $(a).css('font-weight','normal');
});
#textarea { background-color: #fff;
    border: 1px solid #ccc;
    color: #555;
    font-size: 14px;
    height: 34px;
    width: 450px;}
    
#textarea-show{font-size: 2rem;
  color:#666;
  height:50px;
  border: 1px solid #ccc;
  width: 450px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="bld">B</button>
<button class="itl">I</button>
<div id="textarea" contenteditable></div>
<div id="textarea-show"></div>

Update - 要选择文本 bolditalic,您需要按照 [=23= 的建议使用 document.execCommand ],

When an HTML document has been switched to designMode, the document object exposes the execCommand method which allows one to run commands to manipulate the contents of the editable region.

$('#textarea').keyup(function() {
  $('#textarea-show').html($(this).text());
});
$(".bld").on('click',function(){
 document.execCommand('bold');
  var a = $("#textarea").html();
 $('#textarea-show').html(a);
});

$(".itl").on('click',function(){
 document.execCommand('italic');
  var a = $("#textarea").html();
 $('#textarea-show').html(a);
});
#textarea { background-color: #fff;
    border: 1px solid #ccc;
    color: #555;
    font-size: 14px;
    height: 34px;
    width: 450px;}
    
#textarea-show{font-size: 2rem;
  color:#666;
  height:50px;
  border: 1px solid #ccc;
  width: 450px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="bld">B</button>
<button class="itl">I</button>
<div id="textarea" contenteditable></div>
<div id="textarea-show"></div>

您可以添加两个 类,假设一个 bold 和另一个 italic,设置它们的样式并在单击按钮时将它们切换到 activate/deactivate bold/italic(您可以 运行 下面的代码片段,或者您也可以找到更新后的 jsfiddle here):

更新

在 OP 发表评论后,因为他只想为选定的文本添加粗体和斜体,所以我稍微更新了我的答案。

已更新jsfiddle.

更新后的代码:

$('#textarea').keyup(function() {
  $('#textarea-show').html($(this).text());
});

$('#bold_btn').on('click', function() {
  //$('#textarea, #textarea-show').toggleClass('bold');
  document.execCommand('bold');
  var text = document.getElementById('textarea').innerHTML;
  $('#textarea-show').html(text);
});
$('#italic_btn').on('click', function() {
  //$('#textarea, #textarea-show').toggleClass('italic');
  document.execCommand('italic');
  var text = document.getElementById('textarea').innerHTML;
  $('#textarea-show').html(text);
});
#textarea {
  background-color: #fff;
  border: 1px solid #ccc;
  color: #555;
  font-size: 14px;
  height: 34px;
  width: 450px;
}
#textarea-show {
  font-size: 2rem;
  color: #666;
  height: 50px;
  border: 1px solid #ccc;
  width: 450px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='bold_btn'>B</button>
<button id='italic_btn'>I</button>
<div id="textarea" contenteditable></div>
<div id="textarea-show"></div>