【发布时间】:2017-12-24 03:51:07
【问题描述】:
当我们点击 glyphicon 时,我需要一个功能来动态输入文本区域。
$(document).ready(function(){
//Click Event of glyphicon class
$(document).on('click','.glyphicon',function(){
var previous_content = $.trim($("#area").html());
var new_icon = '<span class="'+$(this).attr('class')+'"></span>';
//*****The below method of create element is also not working
//var new_icon = document.createElement("span");
//new_icon.setAttribute('class', $(this).attr('class'));
var new_content = previous_content+new_icon;
$("#area").html(new_content);
});
});
.glyphicon {
cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<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>
搜索前请使用sn-p查看实际问题。
【问题讨论】:
-
请向下滚动 sn-p 以查看可点击的字形图标。
-
你不能直接在文本框中插入图标......你应该去ASCII格式
-
@sunil:我正在制作一个自定义编辑器。你能举个例子吗?因为我认为它应该工作
-
@sunil 我们可以把它想象成表情符号。就像我们将表情符号添加到 textarea 的内容中一样。就像那样
标签: javascript jquery html bootstrap-4 glyphicons