我试图将文本I PASTE单独插入Textarea
这是我的代码
$(document).ready(function(){
$("#span").click(function() {
alert("1. all words into separate span, 2. all words gone after that ");
$('#words').each(function(){
var words = $(this).text().split(" ");
var total = words.length;
$(this).empty();
for (index = 0; index < total; index ++){
$(this).append($("<span /> ").text(words[index]));
}
});
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" name="" value="Click" id="span"> <br><br>
<textarea id="words" autocomplete="off" spellcheck="false" autocapitalize="off" autocorrect="off" >It works only when I put text in here, not when I paste it</textarea>
&#13;
答案 0 :(得分:0)
您的选择器不正确。$('#words')选择器仅用于一个对象并且是id选择器。 您应该为所有跨度指定相同的类,并更改您的选择器,如下所示:
$(”。词语)。
答案 1 :(得分:0)
下次提问时,请发布一个有用的描述性标题,并尝试更具描述性。只是粘贴你的代码,并说这里为我做这将得到你的负面回应。彻底解释您尝试过的内容以及您尝试做的事情。下次你问一个问题。
为了帮助你摆脱目前的困境,你可以尝试这样的事情。
$("#span").click(function() {
alert("1. all words into separate span, 2. all words gone after that ");
var textArea = $('#words');
var arrayOfWords = textArea.val().split(" ");
var wordsInSpans = '';
$.each(arrayOfWords, function(i, word){
console.log(word) // you can do anything you want with the word in here
wordsInSpans += '<span>' + word + '</span>';
})
$('body').append(wordsInSpans);
textArea.val(''); // This will set the value to blank
});
祝你好好建立