为什么ASP.Net单选按钮和CheckBox呈现跨度里面呢? [英] Why does ASP.Net RadioButton and CheckBox render inside a Span?

查看:211
本文介绍了为什么ASP.Net单选按钮和CheckBox呈现跨度里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这样的:

<asp:CheckBox    ID="CheckBox1"    runat="server" CssClass="myClass" />
<asp:RadioButton ID="RadioButton1" runat="server" CssClass="myClass" />
<asp:TextBox     ID="TextBox1"     runat="server" CssClass="myClass" />

...来呈现这样的(与某些属性为简单删除)的:

<input id="CheckBox1"    type="checkbox" class="myClass" />
<input id="RadioButton1" type="radio"    class="myClass" />
<input id="TextBox1"     type="text"     class="myClass" />

...其实时,单选复选框获取包裹着跨度标记和CSS类被应用在那里。

...when in fact, the RadioButton and CheckBox get wrapped with a span tag and the CSS class gets applied there.

<span class="myClass"><input id="CheckBox1"    type="checkbox" /></span> 
<span class="myClass"><input id="RadioButton1" type="radio"    /></span> 
<input type="text" id="TextBox1" class="myClass" />

有一个原因,是有办法避免呢?它使jQuery选择丑陋的,因为你不能赶上所有的人都用:

Is there a reason for this and is there a way to avoid it? It makes jQuery selectors ugly since you can't catch all of them with:

$("input.myClass")

就算它只是要:

$("input.myClass, span.myClass input")

...但是,这是丑陋的。我可以写我自己的选择,但同样没有优雅,因为它应该的。

...but that is ugly. I could write my own selector, but again not as elegant as it should be.

推荐答案

System.Web.UI.WebControls 命名空间在不同的浏览器可能会呈现不同的Web控件。你不能指望他们总是呈现相同的元素。他们可以添加任何东西,他们认为是需要让它在特定的浏览器。

Web controls in the System.Web.UI.WebControls namespace may render differently in different browsers. You can't count on them rendering the same elements always. They may add anything that they think is needed to make it work in the specific browser.

如果你想拥有在控件如何呈现为HTML的任何控制,你应该使用控制在 System.Web.UI.HtmlControls 命名空间来代替。这就是:

If you want to have any control over how the controls are rendered as html, you should use the controls in the System.Web.UI.HtmlControls namespace instead. That is:

<input type="checkbox" id="CheckBox1" runat="server" class="myClass" />
<input type="radio" name="RadioButton1" runat="server" class="myClass" />
<input type="text" id="TextBox1" runat="server" class="myClass" />

他们会呈现如同相应的HTML元素,添加了没有多余的元素。当然,这意味着你将不得不采取对浏览器的兼容性的责任,作为控制没有。此外,这些控件不具有在器WebControls 命名空间中的控件的所有功能。

They will render just as the corresponding html element, with no extra elements added. This of course means that you will have to take responsibility for the browser compatibility, as the control doesn't. Also, those controls doesn't have all the features of the controls in the WebControls namespace.

这篇关于为什么ASP.Net单选按钮和CheckBox呈现跨度里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆