[英]How can i insert html element between start and end tag of another html element using jquery
I have a textbox : 我有一个文本框:
<input type="text"></input>
I want to add a span between the input element using jquery like the below: 我想使用jquery在输入元素之间添加跨度,如下所示:
<input type="text"><span>Added span</span></input>
How can I achieve that? 我该如何实现?
I will answer three questions: 我将回答三个问题:
How to append some HTML in an element? 如何在元素中附加一些HTML?
Use jQuery: $('div').append('<span>Added span</span>');
使用jQuery: $('div').append('<span>Added span</span>');
How to add a placeholder to an input element? 如何在输入元素中添加占位符?
Use the following markup: <input type="text" placeholder="My placeholder" />
(Browser support: http://caniuse.com/input-placeholder ) 使用以下标记: <input type="text" placeholder="My placeholder" />
(浏览器支持: http : //caniuse.com/input-placeholder )
How to add text by default in my input? 如何在我的输入中默认添加文本?
Use the following markup: <input type="text" value="My text" />
使用以下标记: <input type="text" value="My text" />
You can't put a <span>
(or any element) into a text input. 您不能将<span>
(或任何元素)放入文本输入中。 Look at this fiddle: http://jsfiddle.net/GAFTk/ 看看这个小提琴: http : //jsfiddle.net/GAFTk/
Your browser will change it to: 您的浏览器会将其更改为:
<input type="text" /><span>Text</span>
and render them side by side. 并排显示它们。
You don't want to put html elements inside of input elements, but in general you use jQuery.append() 您不想将html元素放在输入元素中,但通常使用jQuery.append()
<div id="demo"></div>
<script>$("#demo").append("<span>added span</span>");</script>
Result: 结果:
<div id="demo"><span>added span</span></div>
If you append something else it won't remove whats already in there. 如果您添加其他内容,则不会删除其中已有的内容。
<script>$("#demo").append("<span>another</span>");</script>
<div id="demo"><span>added span</span><span>another</span></div>
If you want to overwrite whats already inside you'd use .html instead of .append, or .empty and then .append 如果您想覆盖已经存在的内容,则可以使用.html而不是.append或.empty然后.append
$('#inputId').innerHtml('<span>adding span</span>');
First: 第一:
<input id="inserthere" type="text"></input>
then: 然后:
$('#inserthere').innerHtml('<span>Added span</span>');
Result: 结果:
<input id="inserthere" type="text"><span>Added span</span></input>
edit: almost forget you can also use the jQuery "append" method if you want to ADD something instead of overwrite it! 编辑:几乎忘了,如果要添加某些内容而不是覆盖它,也可以使用jQuery“ append”方法!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.