如何让我的IE浏览器(和FF)这个getElementsbyName工作? [英] How do I make this getElementsbyName work for IE (and FF)?

查看:142
本文介绍了如何让我的IE浏览器(和FF)这个getElementsbyName工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是差错在IE但在FF(错误的原理JavaScript的 - document.getelementsbyname(...)0.innerhtml为空或不是一个对象。

This is the JavaScript that is errored on in IE but works in FF (error - "document.getelementsbyname(...).0.innerhtml is null or not an object":

var oldVal = parseInt(document.getElementsByName("outSL")[0].innerHTML); //val from DB

这是asp.net code,我想抓住将由数据库中填写了内部HTML:

This is the asp.net code where I want to grab the inner html that will be filled in by the database:

<asp:Label ID="LabelSL" runat="server" Text="" name="outSL" style="visibility:hidden;"></asp:Label>

该ID是动态的,当它被渲染它看起来像这样,它是43我后:

The id is dynamic, when it is rendered it looks like this, where it is the 43 I am after:

<span id="ctl00_cpMainContent_LabelSL" name="outSL" style="visibility:hidden;">43</span>  

所以...我如何才能在IE和FF 43具有相同功能?

So...how can I get that 43 in IE and FF with the same function?

感谢您!

推荐答案

如果您有.NET 4.0,那么你可以写干净的id,然后只用的getElementById,因为作为食肉牛龙州,getElementByName功能不同的跨浏览器。

If you have .NET 4.0 then you can write clean id's and then just use getElementById, since as Carnotaurus state's, getElementByName functions different across browsers.

<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx

如果你被困在.NET 2.0 / 3.5,那么你可以使用jQuery通过任何标识或名称,或许多其他选择访问元素。

If you're stuck on .NET 2.0/3.5, then you can use jQuery to access elements by either Id, or Name, or many other selectors.

http://api.jquery.com/category/selectors/

此外,我写了一个库输出的ID的JSON数组用于.NET 2.0 / 3.5。

Also I wrote a library to output a json array of ID's for .NET 2.0/3.5.

HTTP://awesomeclientid.$c$cplex.com/

http://www.philliphaydon.com/2010/12/i-love-clean-client-ids-especially-with-net-2-0/

这得到各地作INamingContainer拍拍所有的控制RUNAT服务器,它输出像数组:

This gets around the INamingContainer slapped on all the controls runat server, it outputs an array like:

<script type="text/javascript">
//<![CDATA[
var controls = {
"txtUserName": "ctl00_ContentPlaceHolder1_txtUserName",
"txtEmail": "ctl00_ContentPlaceHolder1_txtEmail",
"btnSubmit": "ctl00_ContentPlaceHolder1_btnSubmit"
};
//]]>
</script>

然后就可以通过ID访问的元素,像这样:

Then you can access the elements by ID like so:

var element = document.getElementById(controls.btnSubmit);

这篇关于如何让我的IE浏览器(和FF)这个getElementsbyName工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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