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

前端 未结 2 444
攒了一身酷
攒了一身酷 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:

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

    Then you can access the elements by ID like so:

    var element = document.getElementById(controls.btnSubmit);
    
    0 讨论(0)
  • 2021-01-27 17:36

    GetElementByName functions differently across different browsers. I suggest you access your elements using jQuery.

    0 讨论(0)
提交回复
热议问题