开发者

Multiple instances of (this) referring to different elements in jQuery

开发者 https://www.devze.com 2023-04-13 05:10 出处:网络
So I decided to combine multiple html files into a single file, hiding and showing divs instead of linking each page. I also combined all of the different scripts into a singletag.

So I decided to combine multiple html files into a single file, hiding and showing divs instead of linking each page. I also combined all of the different scripts into a single tag.

However, not all of the code runs anymore - specifically functions referring to "(this)" - is this because it can o开发者_C百科nly be used once per page?

Thanks.


no. "this" depends on the context of the javascript in question. no combination of JS files will ever affect the context of "this". "this" only has meaning inside of a function or method. placing methods or function next to each other in no way nests them inside of each other.

what is more likely to have happened is a polluted global namespace. if, for example, two files contain a variable named var link then whichever code appears later will take precedence.


No. this refers to the DOM element invoked. In the jQuery realm this is utilised with the jQuery constructor function $() to become $(this), which basically lets you utilise jQuery functionality on an invoked DOM element.

I suspect you have some other conflicting issues.


You could use jQuery.proxy function to change the this scope of any of your functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消