jQuery selector does not work in IE7/8

人走茶凉 提交于 2019-12-31 07:29:06

问题


Does anyone know why this would not work in IE7/8?

drop_area = $('div#drop_area');

It works perfectly in IE9, FF2/3, and Chrome. Internet Explorer 7/8 gives the following error:

SCRIPT438: Object doesn't support this property or method 

Edit: This is the HTML that goes with my javascript: http://pastebin.com/nwxx8RzW


回答1:


The code you've shown on pastebin has numerous global variable issues. In other words, you are coding assuming that variables you are declaring are local in scope, whereas in reality they turn out to be global. Examples include set, box_handle, elements, i, id, drop_area, element, row, image_id, etc. All of your functions are global in scope as well, when they can easily be encapsulated in an other function.

Now I don't know if there's some subtle interactions going on, whether some code has hammering (global) data set by other code, but it certainly seems as if something is getting overwritten and hence methods and properties are disappearing. I would start by going through the code and adding var to local variables. Next I'd be encapsulating most of this code in an anonymous autoexecuting function.




回答2:


IE has a weird behaviour to register some properties in global scope. Elements with an given ID may be accessed simply by using the ID.

So you have a element with the ID "drop_area", it's accessible in IE by using this ID, try:

alert(drop_area.tagName)

..to check it.(should give "DIV")

So what happens: You try to assign something else to this element when using drop_area = $('div#drop_area'); , but this is an invalid operation on an DOMElement.

So use the var-keyword to clarify that you want to create a variable

var drop_area = $('div#drop_area');

or in the case that you have to create a global variable inside a function, assign the variable to the global context:

window['drop_area'] = $('div#drop_area');



回答3:


Usually that error shows, that you use jQuery on a website that also uses Prototype. That's why get an error (which is actually throw by Prototype). The other possibility is, that you try to call the code, before the jQuery lib was included into the HTML.

To make sure it's not my first guess, add this code to your JS code:

$.noConflict();

Therefore it is important that Prototype is included into the HTML, before jQuery is included: http://api.jquery.com/jQuery.noConflict/

If you than replace all occurrences of $() with jQuery() and it works, it was the first issue with using jQuery and Prototype at the same time.




回答4:


Have you got an element with an id of 'drop_area'? ie 6/7/8 auto assigns a global var to the dom element using the element id. Some more code would be helpful.



来源:https://stackoverflow.com/questions/6528093/jquery-selector-does-not-work-in-ie7-8

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