php 为什么 textarea 充满了神秘的空白?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2202999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is textarea filled with mysterious white spaces?
提问by Afamee
I have a simple text areain a form like this:
我有一个像这样形式的简单文本区域:
<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink">
<?php if($siteLink_val) echo $siteLink_val; ?>
</textarea>
I keep getting extra white space in this textarea. When I tab into it my cursor is like in the middle of the textareaand not in the beginning? What is the explanation?
我在这个textarea 中不断获得额外的空白。当我进入它时,我的光标就像在textarea的中间而不是在开头?解释是什么?
回答by Pekka
Look closely at your code. In it, there are already three line breaks, and a ton of white space before </textarea>. Remove those first so that there are no line breaks in between the tags any more. It might already do the trick.
仔细看看你的代码。其中,已经有三个换行符,并且在</textarea>. 首先删除那些,以便标签之间不再有换行符。它可能已经成功了。
回答by amarillion
Well, everything between <textarea>and </textarea>is used as the default value for your textarea box. There is some whitespace in your example there. Try to eliminate all of that.
好吧,<textarea>和之间的所有内容都 </textarea>用作 textarea 框的默认值。您的示例中有一些空格。尝试消除所有这些。
回答by Bart Kiers
Open (and close!) your PHP tags right after, and before, your textareatags:
在您的textarea标签之后和之前打开(和关闭!)您的 PHP标签:
<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php
if($siteLink_val) echo $siteLink_val;
?></textarea>
回答by pega wega
In short:
<textarea>should be closed immediately on the same line where it started.
简而言之:
<textarea>应该在它开始的同一行上立即关闭。
General Practice:this will add-up line-breaks and spaces used for indentation in the code.
一般做法:这将在代码中添加用于缩进的换行符和空格。
<textarea id="sitelink" name="sitelink">
</textarea>
Correct Practice
正确做法
<textarea id="sitelink" name="sitelink"></textarea>
回答by beebek
Basically it should be
基本上应该是
<textarea>something here with no spaces in the begining</textarea>
If there are some predefined spaces lets say due to code formatting like below
如果有一些预定义的空格可以说由于代码格式如下
<textarea>.......
....some_variable
</textarea>
The spaces shown by dots keeps on adding on each submit.
点显示的空格在每次提交时不断增加。
回答by Gyan
Any space in between textarea openning and closing tags will be consider as whitespace. So for your above code, the correct way will be :
textarea 开始和结束标签之间的任何空格都将被视为空白。所以对于你上面的代码,正确的方法是:
<textarea style="width:350px; height:80px;" cols="42" rows="5" name="sitelink"><?php if($siteLink_val) echo $siteLink_val; ?></textarea>
回答by Milad.Nozari
Another work around would be to use javascript:
另一种解决方法是使用 javascript:
//jquery
$('textarea#someid').html($('textarea#someid').html().trim());
//without jquery
document.getElementById('someid').innerHTML = document.getElementById('someid').innerHTML.trim();
This is what I did. Removing white-spaces and line-breaks in the code makes the line too long.
这就是我所做的。删除代码中的空格和换行符会使行太长。
回答by Brian Lacy
To make it look a bit cleaner, consider using the ternary operator:
为了使它看起来更简洁,请考虑使用三元运算符:
<textarea><?=( $siteLink_val ? $siteLink_val : '' );?></textarea>
回答by User707
<textarea style="width:350px;
height:80px;" cols="42" rows="5" name="sitelink"
><?php if($siteLink_val) echo $siteLink_val; ?></textarea>
Moving ...> down works for me.
移动...>向下对我有用。
回答by William Hou
I got the same problem, and the solution is very simple: don't start a new line!Although some of the previous answers can solve the problem, the idea is not stated clearly. The important understanding is, to get rid of the unintended spaces, never start a new line just after your start tag.
我遇到了同样的问题,解决方法很简单:不要换行!前面的一些回答虽然可以解决问题,但是思路没有说清楚。在重要的认识是,摆脱无意识的空间,永远只是你的开始标记后开始新的一行。
The following way is WRONGand will leave a lot of unwanted spaces before your text content:
以下方式是错误的,会在您的文本内容之前留下很多不需要的空格:
<textarea>
text content // start with a new line will leave a lot of unwanted spaces
</textarea>
The RIGHT WAY to do it is:
正确的做法是:
<textarea>text content //put text content right after your start tag, no new line
</textarea>

