[英]How can I border each word in jquery in this situation?
Click http://jsfiddle.net/18zdmp4b/1/ to see result. 点击http://jsfiddle.net/18zdmp4b/1/查看结果。 Enter value in the input, press Enter key, the value will be border.
在输入中输入值,按Enter键,该值将为border。 However, I want to border each word alone.
但是,我想单独对每个单词进行边界处理。 If I enter banana, apple and banana should be separate in two different rectangle.
如果我输入香蕉,苹果和香蕉应分成两个不同的矩形。 How can I achieve that?
我怎样才能做到这一点? appreciate.
欣赏。
$( "#input" ).keydown(function( event ) { //press Enter key if ( event.which == 13 ) { event.preventDefault(); //put input value into div var value=$('#input').val(); $('#test').append(" "+value); $('#test').css('border-style','solid'); } });
.test {display:inline-block;}
<div class="test" id="test">apple</div> <input type="text" id="input"/>
You will need to split
the value
on whitespace, loop over the words, and create individual HTML tags for each word. 您需要在空格上
split
value
,循环显示单词,并为每个单词创建单独的HTML标记。
$( "#input" ).keydown(function( event ) { //press Enter key if ( event.which == 13 ) { event.preventDefault(); //put input value into div var value = $('#input').val(); var words = value.split(/\\s/); for ( var i = 0; i < words.length; i++ ) { var word = words[i]; if ( word ) { $('#test').append(' ');//Add the space between tags (optional). $('<span></span>') .text(words[i]) .css('border-style','solid') .appendTo('#test'); } } } });
.test {display:inline-block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="test" id="test">apple</div> <input type="text" id="input"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.