“SCRIPT5002 function expected” error in IE

2019-02-28 07:16发布

问题:

i am facing an issue and getting error like "SCRIPT5002 function expected" in internet explorer 7-9. this is my code :

 var myDiv = document.getElementById("divId"); //this line gives me "SCRIPT5002 function expected" error.

 myDiv.style.cssText("position:absolute;z-index:999");
myDiv.appendChild(
        JavaScriptCode);

so how to solve this??

回答1:

The problem should be in the 2nd line:

myDiv.style.cssText("position:absolute;z-index:999");

cssText is not a function, but a property. So call it like this:

myDiv.style.cssText = "position:absolute;z-index:999";

or (better approach in my opinion, because it is clearer):

myDiv.style.position = 'absolute';
myDiv.style.zIndex = 999;


回答2:

I also got this in an attempt to check if a variable was an Element.

"notAnElement" instanceof Element

And it always throws the function expected error.

document.createElement("div") instanceof Element

Successfully evaluates to true

I haven't implemented it yet, but my solution is to use a try/catch block.