文本区域默认文本未正确对齐

发布于 2024-11-15 10:05:31 字数 615 浏览 3 评论 0原文

我下面有一个文本区域。当我运行代码时,刷新时文本区域中的文本未左对齐且位于顶部。这阻止我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我鬧 2024-11-22 10:05:31

相反,您可以在与“回答这个问题...”进行比较之前修剪文本区域的值。

例如

if($.trim($('#box').val()) == 'Answer this problem...') { /* do something */ }

Instead, you can trim your value of the textarea before comparing it with "Answer this problem...".

For instance

if($.trim($('#box').val()) == 'Answer this problem...') { /* do something */ }
何以笙箫默 2024-11-22 10:05:31

嗯,您的 textarea 开始标记和 PHP echo 内容之间有很多空白。

<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>

Well, you have a lot of white-space between your textarea start tag and your PHP echo content.

<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>
贱贱哒 2024-11-22 10:05:31

文本区域中的所有空白实际上都呈现为内容(有点像

 标签内)。

因此,为了避免这种情况,只需在关闭

<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>

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 ...>:

<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>
2024-11-22 10:05:31

这是因为您从新行和选项卡中添加了额外的空间。相反,您会想要这样:

<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>

或者,我建议在比较之前修剪值(删除前导/尾随空格)。使用 jQuery 的 .trim() 函数可以轻松做到这一点。

Its because you are adding extra space from your new line and tabs. Instead you would want this:

<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>

Alternatively, I would recommend trimming the values before comparison (removing leading/trailing whitespace). This is easy to do with jQuery's .trim() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文