object expected error javascript

流过昼夜 提交于 2019-12-24 07:30:59

问题


<script type="application/javascript" language="javascript">
    function showElement(elementID, show){
    var element = document.getElementById(elementID);
    if (element) {
      element.className = (show ? element.className.replace(/hidden/gi, "show") : element.className + " hidden");
     }
    }
</script>

        <table cellpadding="3" cellspacing="1" border="0" width="100%">  
            <tr class="baseGrayMedium">
                <td colspan="2">
                    (<a href="javascript:void(0);" onClick="showElement('evicChkLst',true);" class="nostyle">+</span></a>|<a href="javascript:void(0);" onClick="showElement('evicChkLst',false);" class="nostyle">-</span></a>) &nbsp;&nbsp; <B>Eviction Checklist</B>
                </td>
            </tr>
        </table>

I get the javascript error saying object expected and it points to onClick event in the HTML code.. Could some one suggest me why so

EDIT:

<script type="application/javascript" language="javascript">
                function showElement(elementID, show){
                    var element = document.getElementById(elementID);
                    if (element) {
                        element.className = (show ? element.className.replace(/hidden/gi, "show") : element.className + " hidden");
                    }
                }
            </script>

            <table cellpadding="3" cellspacing="1" border="0" width="100%">     
                <tr class="baseGrayMedium">
                    <td colspan="2">
                        (<a href="#" onclick="javascript:showElement('evicChkLst',true);" class="nostyle">+</span></a>|<a href="#" onclick="javascript:showElement('evicChkLst',false);" class="nostyle">-</span></a>) &nbsp;&nbsp; <B>Eviction Checklist</B>

                    </td>
                </tr>
            </table>

Now the code looks some thing like the above


回答1:


Your problem is: <script type="application/javascript" language="javascript"> It must be <script type="text/javascript" language="javascript">




回答2:


I don't immediately see anything wrong in your snippet.

It's possible that formatting elsewhere in your script has messed up the definition or scope of showElement. Try adding this link next to the others:

<a href="javascript:void(0)" onclick="alert(typeof showElement);">?</a>

It should alert function if everything up to that point is good (or, at least, not alert undefined).




回答3:


I just had this error and it was because I had a form element that was trying to submit to an action that was invalid.

Mine was and that was not a valid action.

If you are still having this error you should verify syntax as well as that your form is submitting to a valid method.

This error indicates bad html syntax or invalid source.



来源:https://stackoverflow.com/questions/1676029/object-expected-error-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!