简体   繁体   English

如何将div放在输入类型文本中

[英]How to put div inside input type text

How can i put a div inside text field of a html form 如何将div放在html表单的文本字段中

<h2>Skills</h2>
<label for="skills">Choose your skills</label>
<input id="skills" type="text"/>

Please note my english is not good. 请注意我的英语不好。 So i can't tell you more. 所以我不能告诉你更多。

I just want to render each skill in each div. 我只想渲染每个div中的每个技能。

my jquery code(separated by comma) is : 我的jquery代码(以逗号分隔)是:

$("input#skills").keydown(function(e){
   if(e.which===188){
      var skbl = $.trim($("input#skills").val());
      $("input#skills").append("<div style='background:green;'>"+skbl+"</div>");
});

You can't place any element inside an input. 您不能在输入中放置任何元素。 An input does not contain any HTML. 输入不包含任何HTML。

If you want to add the div inside the text field such as input or textarea then try this: 如果要在文本字段中添加div ,例如inputtextarea尝试以下操作:

$("input#skills").val("<div style='background:green;'>"+skbl+"</div>");

This will update the whole field. 这将更新整个领域。 You can try out this: 你可以尝试这个:

$("input#skills").append(new_val); 

Note: 注意:

You can use HTML value in a input . 您可以在input使用HTML值。 But you will get other server side issues. 但是你会得到其他服务器方面的问题。 So that's why I guess Colandus was asking you not to write it there, however his idea is good to. 所以这就是为什么我猜Colandus要求你不要在那里写,但是他的想法很好。 Because you won't get to know how to edit it, and the browser would mess up the UI! 因为你不会知道如何编辑它,浏览器会弄乱UI!

您不能在输入元素中放置任何其他元素,请参阅html-spec: http//www.w3.org/TR/REC-html40/interact/forms.html#h-17.4

<!ELEMENT INPUT - O EMPTY -- form control -->

The way to do that is to put the input and the wanted div inside a wrapper div, which should look like the input - the wanted width, length of the input and should have border. 这样做的方法是将输入和所需的div放在一个包装div中,它应该看起来像输入 - 所需的宽度,输入的长度,并且应该有边框。 The input inside should be inline-block, without border and with smaller width and the div inside should be again inline-block, without border and with width, filling the rest of the width of the outside div. 内部输入应该是内联块,没有边框,宽度较小,div内部应该是内嵌块,没有边框和宽度,填充外部div的其余宽度。 In this way you can make an image in input - the image is in the inside div, or clickable button inside the input. 通过这种方式,您可以在输入中创建图像 - 图像位于内部div中,或输入内部的可单击按钮。

See here: Clickable icon inside input field 请参阅此处: 输入字段内的可点击图标

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

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