2016-12-09 31 views
2

我想與加粗斜體使用相同的選項在keyup上顯示另一個div中的內容的contenteditable div。 我設法顯示文本,但沒有選項。請幫助Contenteditable 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/

+0

17anchi,請參閱我的回答如下。我希望那是你需要的。 – Ionut

回答

1

您可以添加兩個班,一個假設bold和其他italic,風格他們的toogle他們上的按鈕來激活的點擊/關閉黑體/斜體(您可以運行下面的代碼片段,或者您也可以找到更新的jsfiddle here):

UPDATE

的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>

+0

有沒有辦法在選定的文本上應用選項? tnx – 17anchi

+0

是的。看到我更新的答案。例如,您可以使用'document.execCommand('bold');'。 – Ionut

+0

它的工作原理!這正是我需要的。非常感謝! – 17anchi

0

你必須使用爲了使選定的文本bolditalic你需要使用document.execCommand - 10 font-stylefont-weightoutput 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>

更新轉換如@Ionut,

所示

當HTML文檔切換到designMode時,文檔 對象公開了execCommand方法,該方法允許運行命令 來操作可編輯區域的內容。

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

 
$(".itl").on('click',function(){ 
 
\t document.execCommand('italic'); 
 
    var a = $("#textarea").html(); 
 
\t $('#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>

+0

@ 17anchi希望這可以工作。 – frnt

+1

有沒有辦法在選定的文本上應用選項?tnx – 17anchi

+0

@ 17anchi你的意思是添加大膽和斜體的權利,所以這是很好的在這裏點擊b或i按鈕,這是用戶的選擇。或者你在問別的東西。 – frnt