如何在输入标签内显示div?

Man*_*noz 5 javascript css jquery

我正在尝试为我的网站创建像stackoverflow这样的标签,我网站上的用户将创建用于过滤结果的标签或许多其他操作,如搜索,专业知识等.

我能够创建标签,但不能像在stackoverflow中那样在输入框中显示它,标签之间的边距有问题.每当我创建标签时,它都是对齐的,但没有空格或边距.目前,它显示内联所有标签,没有任何空格.

jQuery我试过 -

$(function () {        
    $("#inputtext").keypress(function (e) {
        var valueofinput = $(this).val();
        if (e.which == 13) {
            $('.tag').html(valueofinput);
            $('.tag').show();
        }
    });

    $('.tag').click(function () {
        $(this).hide();
    });        
});    
Run Code Online (Sandbox Code Playgroud)

但是我尝试在输入标签中显示它,如下所示 -

if(e.which==13)
{
$("#inputtext").val().addClass('tag').css('display','inline-block');
}
if(e.which==32)     //for space
{
$('.tag').css('margin-left','5px');
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

的jsfiddle

mus*_*ing 9

简答:你不能/不能.但你可以给出你的样子.

更长的回答:例子

$(function(){    
    $("#inputtext").keypress(function(e){
        var valueofinput= $(this).val();
        if(e.which==13)
        {
            $('#inputtext').before('<span class="tag">' + valueofinput + '</span>');
            $('input').val('');
        }
    });
    $(document).on('click', '.tag', function(){
        $(this).remove();
    });
    $('.inputholder').click(function() { $('#inputtext').focus(); });
});
Run Code Online (Sandbox Code Playgroud)