我知道如何填充文本区域,但如何填充它以使其保持换行符?
例如
html
<div id="previous_purchases">blah blah blah<br />blah blah</div>
jquery
$('#previous_purchases').click(function(){
var what = $(this).text();
$('#purchased').text(what);
});
所有的废话都在文本区域的一行中出现。有什么想法吗?
编辑:我一直在尝试使用 html() 而不是文本,但它产生相同的结果。我想通过使用 html() 我最终会得到一个带有“
”的文本区域,但事实并非如此......它只是全部在一行上。
即使使用此代码:
$('#previous_purchases').click(function(){
var what = $(this).html();
$('#purchased').html(what);
});
最佳答案
尝试这样做:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(
function() {
$('#previous_purchases').click(function() {
var what = $(this).html().replace(/<br>/, '\n');
$('#purchased').html(what);
});
});
</script>
</head>
<body>
<div id="previous_purchases">blah blah blah<br />blah blah</div>
<textarea id="purchased" cols="25" rows="10"></textarea>
</body>
</html>
关于jquery - 如何使用 jquery 和换行符填充文本区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1242255/