admin管理员组文章数量:1293787
I have several links which all use a similar function, so I want to call the function and pass it attributes from the object that made the call. How do I do this?
This is my code:
$('.flash').click(function(){
getDimensions($(this));
swfobject.embedSWF(fileRef, "myContent", fileWidth, fileHeight, "9.0.0");
});
function getDimensions(linkRef){
fileHeight = $(linkRef).attr('data-height');
fileWidth = $(linkRef).attr('data-width');
}
Am I just referencing $(this)
improperly?
I have several links which all use a similar function, so I want to call the function and pass it attributes from the object that made the call. How do I do this?
This is my code:
$('.flash').click(function(){
getDimensions($(this));
swfobject.embedSWF(fileRef, "myContent", fileWidth, fileHeight, "9.0.0");
});
function getDimensions(linkRef){
fileHeight = $(linkRef).attr('data-height');
fileWidth = $(linkRef).attr('data-width');
}
Am I just referencing $(this)
improperly?
- Aside from the fact that the two functions municate using globals (bad!), what isn't working? It looks like it should work. – Jon Commented Apr 12, 2011 at 22:17
- how would you write it without globals? I think that's my problem actually - I need that function to return the fileWidth and fileHeight and carry out the rest of the click function – mheavers Commented Apr 12, 2011 at 22:19
3 Answers
Reset to default 6$('.flash').click(function(){
var dimensions = getDimensions($(this));
swfobject.embedSWF(fileRef, "myContent", dimensions.fileWidth, dimensions.fileHeight, "9.0.0");
});
function getDimensions(linkRef){
return {
fileHeight: $(linkRef).attr('data-height'),
fileWidth: $(linkRef).attr('data-width')
}
}
Without globals, you can return a JSON object:
$('.flash').click(function(){
var d = getDimensions($(this));
swfobject.embedSWF(fileRef, "myContent", d.width, d.height, "9.0.0"); }
);
function getDimensions(linkRef){
return { "height" : $(linkRef).attr('data-height'),
"width" : $(linkRef).attr('data-width') }
}
Since you are already passing a jQuery object to getDimensions function, you don't need to wrap the argument, linkRef, in another $()
function inside the getDimensions function.
Use this instead:
function getDimensions(linkRef){
fileHeight = linkRef.attr('data-height');
fileWidth = linkRef.attr('data-width');
}
本文标签: javascriptHow to pass an object reference to a function with jQueryStack Overflow
版权声明:本文标题:javascript - How to pass an object reference to a function with jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741586935a2386900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论