奇怪的html控件textarea

虽然一直没怎么用过html控件textarea,但是根据自己一贯的作风,楼猪大胆认为textarea和服务器控件TextBox的TextMode为MultiLine的文本框在最终生成的html标签上应该是一样的。也许是先入为主吧,在上周的一个设计页面中,楼猪毫不思索地写下了下面的html代码:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt < textarea  id ="txtTest1"  value ="123"  cols ="20"  rows ="2" > textarea >

然后,vs有一个提示:消息 1 验证 (XHTML 1.0 Transitional): 属性“value”不是元素“textarea”的有效属性。
晕,textare难道竟然没有value属性?然后楼猪又尝试将textarea放在服务端:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt < textarea  id ="txtTest1"  cols ="20"  rows ="2"  enableviewstate ="false"  runat ="server" > textarea >

然后,在cs文件下,可以写如下代码:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt      this .txtTest1.Value  =   " 123 " ;

这说明服务端的Value属性是存在的,楼猪又查了一下Value属性元数据说明:获取或设置在 System.Web.UI.HtmlControls.HtmlTextArea 控件中输入的文本。再查看一下生成的html源码:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt < textarea name = " txtTest1 "  id = " txtTest1 "  cols = " 20 "  rows = " 2 " > 123 textarea >

现在一目了然。原来textarea的“value”值在textarea开始和结束标签内,而不是像type为text的input,看起来是将value放在value属性上。然后楼猪又用服务器控件TextBox和runat为server的textarea又进行了一下对比:

ExpandedBlockStart.gif 代码
         < asp:TextBox  ID ="txtTest"  TextMode ="MultiLine"  EnableViewState ="false"  runat ="server" > asp:TextBox >
        
< textarea  id ="txtTest1"  cols ="20"  rows ="2"  enableviewstate ="false"  runat ="server" > textarea >

cs下写下如下代码:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt            this .txtTest.Text  =   " 123 " ;
          
this .txtTest1.Value  =   " 123 " ;

它们生成的html也是一样的:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt          < textarea  name ="txtTest"  rows ="2"  cols ="20"  id ="txtTest" > 123 textarea >
        
< textarea  name ="txtTest1"  id ="txtTest1"  cols ="20"  rows ="2" > 123 textarea >

而对于TextBox而言,它又是可以直接在标签上写Text属性的,而runat="server"的textarea却不可以直接在标签内写Value属性:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt < asp:TextBox  ID ="txtTest"  TextMode ="MultiLine"  Text ="123"  EnableViewState ="false"  runat ="server" > asp:TextBox >

然后,楼猪又通过javascript来获取它们的value:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt          var  txtValue = document.getElementById( " txtTest " ).value;
        alert(txtValue);
        
var  txtValue1 = document.getElementById( " txtTest1 " ).value;
        alert(txtValue1);

 这一次它们又都弹出了值123!这就说明textarea是有value属性的(换成innerText属性也一样)。真的彻底服了。最后楼猪为了保险起见,将textarea换成了自己一直使用习惯的没有视图状态的TextBox,因为现在不熟悉也吃不透这个textarea还有哪些古怪的设计。期待指点。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-664092/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-664092/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值