文本区域默认文本未正确对齐
我下面有一个文本区域。当我运行代码时,刷新时文本区域中的文本未左对齐且位于顶部。这阻止我使用 javascript onfocus 和 onblur 事件,因为当我比较框中的字符串时,有额外的空格使其与“回答这个问题...”不同,有没有办法可以制作这个默认文本向左对齐并在文本区域的顶部对齐,没有任何额外的空格,我认为这些空格是从 php 中以某种方式生成的。
代码:
<textarea id ='box' autocomplete='off'>
<?php
if (!$_SESSION['username']){
echo "readonly='readonly'";
}
?>
>
<?php
if (!$_SESSION['username']){
echo "Login to answer...";
}
else {
echo "Answer this problem...";
}
?>
</textarea>
在标题中:
#answerbox{
height: 200px;
width: 400px;
}
I have a textarea below. When I run the code, the text that is suppose to be in the textarea on refresh isn't left aligned and on the top. This is preventing me from using the javascript onfocus and onblur events, because when I compare the string in the box there are extra spaces that are making it different from "Answer this problem..." Is there a way I can make this default text be aligned left and on the top of the textarea without any extra whitespace that I think is being generated from the php somehow.
code:
<textarea id ='box' autocomplete='off'>
<?php
if (!$_SESSION['username']){
echo "readonly='readonly'";
}
?>
>
<?php
if (!$_SESSION['username']){
echo "Login to answer...";
}
else {
echo "Answer this problem...";
}
?>
</textarea>
in the header:
#answerbox{
height: 200px;
width: 400px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
相反,您可以在与“回答这个问题...”进行比较之前修剪文本区域的值。
例如
Instead, you can trim your value of the textarea before comparing it with "Answer this problem...".
For instance
嗯,您的
textarea
开始标记和 PHPecho
内容之间有很多空白。Well, you have a lot of white-space between your
textarea
start tag and your PHPecho
content.文本区域中的所有空白实际上都呈现为内容(有点像
因此,为了避免这种情况,只需在关闭
All whitespaces in a textarea are actually rendered as content (a bit like within a <pre> tag).
So to avoid this, just start your dynamic content directly after closing the <textarea ...>:
这是因为您从新行和选项卡中添加了额外的空间。相反,您会想要这样:
或者,我建议在比较之前修剪值(删除前导/尾随空格)。使用 jQuery 的
.trim()
函数可以轻松做到这一点。Its because you are adding extra space from your new line and tabs. Instead you would want this:
Alternatively, I would recommend trimming the values before comparison (removing leading/trailing whitespace). This is easy to do with jQuery's
.trim()
function.