當我們點擊glyphicon時,我需要一個功能來動態地輸入textarea。在textarea中添加glyphicons顯示的是文本而不是glyphicon
$(document).ready(function(){
//Click Event of glyphicon class
\t \t $(document).on('click','.glyphicon',function(){
\t \t \t var previous_content = $.trim($("#area").html());
\t \t \t var new_icon = '<span class="'+$(this).attr('class')+'"></span>';
\t \t \t //*****The below method of create element is also not working
//var new_icon = document.createElement("span");
\t \t //new_icon.setAttribute('class', $(this).attr('class'));
\t \t \t var new_content = previous_content+new_icon;
\t \t \t $("#area").html(new_content);
\t \t
\t \t });
});
.glyphicon {
\t cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
\t <title>Test</title>
\t <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
\t <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<textarea id="area" rows="10" cols="10"></textarea>
<p>Asterisk icon: <span class="glyphicon glyphicon-asterisk"></span></p>
<p>Copyright-mark icon: <span class="glyphicon glyphicon-copyright-mark"></span></p>
</body>
</html>
搜索之前,請使用片斷看到實際的問題。
請向下滾動片斷看點擊glyphicons。 –
你不能直接在文本框中插入圖標....你應該去ASCII格式 – sunil
@sunil:我正在做一個自定義編輯器。你能舉個例子嗎?因爲我認爲它應該可以工作 –