StackOverflow如何创建其标记系统。如何在文本区域中格式化html?
我只是不知道从哪里开始。如果有人可以指导我,那么我可以知道要采取的方向,这样我就可以编写一些人可以检查的代码。
喜欢这样:
修改: 基本上,当我按空格键时,如何在div的内部添加一个新元素/ div?
答案 0 :(得分:19)
我要做的是:
当您在输入内部写入并假设您选择HTML时,它会在左侧DIV中创建一个范围,并减小右侧div的宽度以匹配剩余大小。在corse中,在你的span中,有带删除符号的文本HTML。
你可以使用jQuery轻松完成。
示例:
<div id="master_div">
<div id="categories">
</div>
<div id="input">
<input type="text" value="" />
</div>
</div>
你写了一些jQuery / JS
答案 1 :(得分:17)
Bootstrap解决方案怎么样?
你可以试试这个:
HTML代码:
<input type="text" value="html,input,tag" data-role="tagsinput"></input>
对于CSS,请调用这两个文件:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="http://bootstrap-tagsinput.github.io/bootstrap-tagsinput/dist/bootstrap-tagsinput.css">
对于Javascript,请调用以下两个文件:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://bootstrap-tagsinput.github.io/bootstrap-tagsinput/dist/bootstrap-tagsinput.min.js"></script>
代码将生成以下结果:
答案 2 :(得分:7)
没有。如果你看看DOM,你最初只看到一个输入框。添加标记后,它会在带有该标记的输入框之前插入<span>
标记,并将其删除图标。输入框现在位于此<span>
标记的右侧。当您单击标签进行编辑时,它会将文本框放在其位置,以便您进行编辑。所有这些都是通过Javascript完成的,并且有一些JQuery插件可以帮助您完成此任务。这是Google快速搜索中的一个:http://xoxco.com/projects/code/tagsinput/
就样式而言,<span>
元素可以包含您想要的任何CSS。
答案 3 :(得分:2)
var es = document.querySelectorAll('.input-categories');
for (var i = 0; i < es.length; i++) {
es[i]._list = es[i].querySelector('ul');
es[i]._input = es[i].querySelector('input');
es[i]._input._icategories = es[i];
es[i].onkeydown = function(e){
var e = event || e;
if(e.keyCode == 13) {
var c = e.target._icategories;
var li = document.createElement('li');
li.innerHTML = c._input.value;
c._list.appendChild(li);
c._input.value = '';
e.preventDefault();
}
}
}
*{
margin: 0px;
padding: 0px;
}
.input-categories{
display: flex;
flex-direction: row;
justify-content: flex-start;
border: 1px solid black;
}
.input-categories ul{
display: flex;
flex-direction: row;
justify-content: flex-start;
list-style-type: none;
flex-wrap: wrap;
}
.input-categories li{
border: 1px solid black;
border-radius: 2px;
padding: 1px;
margin: 1px;
}
.input-categories input{
flex: 1 1 auto;
align-self: flex-start;
}
<div class="input-categories">
<ul>
<li>moscow</li>
<li>new york</li>
</ul>
<input type="text"/>
</div>
<div class="input-categories">
<ul>
<li>CSS</li>
<li>PHP</li>
</ul>
<input type="text"/>
</div>
答案 4 :(得分:0)
const AutoList=()=>{
const [currentTagText, setCurrentTagText] = useState("");
const [tags, setTags] = useState(["javascript"]);
const handleTag = (e) => {
setCurrentTagText(e.target.value);
if (e.keyCode == 13 && currentTagText) {
setTags((prevTags) => [...prevTags, currentTagText]);
setCurrentTagText("");
} else if (e.keyCode == 32 && currentTagText) {
setTags((prevTags) => [...prevTags, currentTagText]);
setCurrentTagText("");
}
};
const removeTag = (index) => {
const newTagArray = tags;
newTagArray.splice(index, 1);
setTags([...newTagArray]);
};
return (
<div className="masterStackDiv">
<div
className="stackTags"
style={{ display: tags.length > 0 ? "flex" : "none" }}
>
{tags.map((tag) => {
return (
<div className="stackTag">
<button
onClick={() => removeTag(index)}
className="tagCloseBtn"
>
x
</button>
#{tag}
</div>
)
})}
</div>
<div className="stackInput">
<input
type="text"
onKeyDown={handleTag}
onChange={handleTag}
value={currentTagText}
/>
</div>
</div>
)
}
CSS:-
.masterStackDiv {
width: auto;
background-color: transparent;
border: none;
display: flex;
align-items: center;
}
.stackInput input[type="text"] {
border: none;
background-color: transparent;
color: white;
font-family: "Segoe UI";
font-size: 0.8em;
float: right;
margin-left: 5px;
width: auto;
border-bottom: 2px solid white;
}
.stackTag {
background-color: #707070;
color: white;
height: 20px;
width: auto;
border-radius: 10px;
font-size: 0.7em;
padding: 5px;
margin: 3px;
display: flex;
align-items: center;
}
.stackTags {
display: flex;
justify-content: center;
align-items: center;
height: 30px;
width: auto;
}
.tagCloseBtn {
border: none;
background-color: transparent;
border-radius: 5px;
}
.tagCloseBtn:hover {
background-color: white;
cursor: pointer;
}
const {useState} = React
const StackTag=()=>{
const [currentTagText, setCurrentTagText] = useState("");
const [tags, setTags] = useState(["javascript"]);
const handleTag = (e) => {
setCurrentTagText(e.target.value);
if (e.keyCode == 13 && currentTagText) {
setTags((prevTags) => [...prevTags, currentTagText]);
setCurrentTagText("");
} else if (e.keyCode == 32 && currentTagText) {
setTags((prevTags) => [...prevTags, currentTagText]);
setCurrentTagText("");
}
};
const removeTag = (index) => {
const newTagArray = tags;
newTagArray.splice(index, 1);
setTags([...newTagArray]);
};
return (
<div className="masterStackDiv">
<div
className="stackTags"
style={{ display: tags.length > 0 ? "flex":"none"}}
>
{tags.map((tag, index) => {
return (
<div className="stackTag" key={index}>
<button
onClick={() => removeTag(index)}
className="tagCloseBtn"
>
x
</button>
#{tag}
</div>
);
})}
</div>
<div className="stackInput">
<input
type="text"
onKeyDown={handleTag}
onChange={handleTag}
value={currentTagText}
/>
</div>
</div>
)
}
ReactDOM.render(<StackTag/>,document.getElementById("react"))
.masterStackDiv {
width: auto;
background-color: transparent;
border: none;
display: flex;
align-items: center;
}
.stackInput input[type="text"] {
border: none;
background-color: transparent;
color: black;
font-family: "Segoe UI";
font-size: 0.8em;
float: right;
margin-left: 5px;
width: auto;
border-bottom: 2px solid black;
}
.stackTag {
background-color: #707070;
color: white;
height: 20px;
width: auto;
border-radius: 10px;
font-size: 0.7em;
padding: 5px;
margin: 3px;
display: flex;
align-items: center;
}
.stackTags {
display: flex;
justify-content: center;
align-items: center;
height: 30px;
width: auto;
}
.tagCloseBtn {
border: none;
background-color: transparent;
border-radius: 5px;
}
.tagCloseBtn:hover {
background-color: white;
cursor: pointer;
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
</head>
<body>
<div id="react"></div>
</body>
</html>