How do I make this getElementsbyName work for IE (and FF)?

前端 未结 2 445
攒了一身酷
攒了一身酷 2021-01-27 16:54

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 =         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-27 17:36

    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.

    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

    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/

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

    http://awesomeclientid.codeplex.com/

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

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

    Then you can access the elements by ID like so:

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

提交回复
热议问题