简体   繁体   English

如何使用css,html和jquery创建标记样式?

[英]How to create a tagging style with css, html and jquery?

I have an input[type=text] element and another div for selected elements in my form. 我在表单中有一个input[type=text]元素和一个用于选定元素的div。

I need to create my own custom tag plugin container and my problem is in css style and layout for the tag container and appearance for it. 我需要创建自己的自定义标签插件容器,而我的问题是标签容器的CSS样式和布局以及外观。

This is my actual stage (very poor): http://jsfiddle.net/db8upk61/1/ 这是我的实际阶段(非常差): http : //jsfiddle.net/db8upk61/1/

I want something like it: 我想要这样的东西: 在此处输入图片说明

challenges 挑战

  • how to simulate an input border between these elements? 如何模拟这些元素之间的输入边界?
  • how to remove border from input element like the example above? 像上面的例子,如何从输入元素中删除边框?
  • how to mantain tags in the same line as input? 如何在与输入相同的行中保存标签?

There are a lot of plugins that makes that, but, as you need to do. 有很多插件可以做到这一点,但是,您需要这样做。

You can create a container simulating an input and inside this container you can put the real input with border white, I made an example, take a look: 您可以创建一个模拟输入的容器,并且可以在该容器内将带有边框白色的真实输入放入,我举了一个例子,看一下:

http://jsfiddle.net/93n2af0e/ http://jsfiddle.net/93n2af0e/

CSS code: CSS代码:

.input-select-tag{
    background-color: #FFFFFF;
    boder: 1px solid #F1F1F1;
    height: 36px;
    width: 450px;
}
.selected-tags{
    padding: 2px;
}
.button{
    float: left;
    padding: 5px;
    margin-left: 2px;
}
.input{
    float: left;
    border: #FFFFFF;
    margin-left: 2px;
    padding: 5px;
}
.clear{
    clear: both;
}

HTML: HTML:

<form class='form-horizontal well'>
    <div class='control-group'>

        <div class='input-select-tag'>
            <div class='selected-tags'>
                <span class='alert button alert-info'>tag1 <a href="javascript:;" title='remove' >x</a></span> 
                <span class='alert button alert-info'>tag2 <a href="javascript:;" title='remove' >x</a></span>          
            </div>

            <input type='text' name='selectTags' class='input' size="40"/>    
            <div class='clear'></div>
        </div>
    </div>
</form>

Hope it helps. 希望能帮助到你。

 var i = 7; $("input").focus(function(e) { // set cursor position if (!this.value.length) { this.value = $.map(Array(i * $(".alert").length), function() { return " " }).join(" ") } }).change(function(e) { // append new "tag" $(".alert").first().clone().text(function(i, text) { return text.replace(/tag1/, e.target.value) }).appendTo($(".alert").parent()); $(this).val("").focus() }).closest("form").submit(function(e) { e.preventDefault() }) 
 <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <form class='form-horizontal well'> <div class='control-group'> <div class='selected-tags'> <span class='alert alert-info'>tag1 <a href="javascript:;" title='remove' >x</a></span> <span class='alert alert-info'>tag2 <a href="javascript:;" title='remove' >x</a></span> </div> <label>Tags</label> <br> <input type='text' name='selectTags' /> </div> </form> <style> .control-group { position: relative; } .alert { position: relative; top: 47px; font-size: 12px; padding: 2px; margin: 2px; } input { white-space: pre; width: 100%; } </style> 

jsfiddle http://jsfiddle.net/db8upk61/2/ jsfiddle http://jsfiddle.net/db8upk61/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM