[英]In jQuery, how to define a function with a selector as a parameter?
I am trying to define a function that would to that:我正在尝试定义一个 function :
var photoWidth = $('#photo_preview').width();
var photoHeight = $('#photo_preview').height();
if (imgHeight > imgWidth){
$('#photo_preview').css('width','100');
}
My goal is to create a function that would be like:我的目标是创建一个像这样的 function:
resizePhoto(theselector);
... with the parameter "theselector" being $('#photo_preview') or any other selector. ...参数“theselector”为 $('#photo_preview') 或任何其他选择器。
NOTE: I can't use classes for this one.注意:我不能为此使用类。
What should I do?我应该怎么办?
function resizePhoto(selector){
var photoWidth = $(selector).width();
var photoHeight = $(selector).height();
if (imgHeight > imgWidth){
$(selector).css('width','100');
}
}
... ...
resizePhoto('#photo_preview'); //Usage
function(selector) {
var $jq = $(selector);
var imgWidth = $jq.width();
var imgHeight = $jq.height();
if (imgHeight > imgWidth){
$jq.css('width','100px');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.