为什么 ASP.Net RadioButton 和 CheckBox 在 Span 内呈现? [英] Why does ASP.Net RadioButton and CheckBox render inside a Span?

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

问题描述

我希望这样:

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

...事实上,RadioButtonCheckBox 被一个 span 标签包裹,并且 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 RadioButton 和 CheckBox 在 Span 内呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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