嘿伙计们。 所以这更多是因为我不了解 JS,而不是一个真正的难题。
我正在尝试用 JS 编写一个小函数,我可以从 jQuery 调用它来调整图像大小。问题是我不知道如何在 JS 函数运行后将值返回给 jQuery。这是我到目前为止所拥有的:
$('a').click(function() {
var slideshowHeight = $(this).closest('.stage').css('height')
var slideshowWidth = $(this).closest('.stage').css('width')
var newImageHeight = $(this).attr('data-height')
var newImageWidth = $(this).attr('data-width')
fit_within_box(slideshowWidth, slideshowHeight, newImageWidth, newImageHeight);
$(this).children('img').css('width',new_width);
$(this).children('img').css('width',new_height);
}
function fit_within_box(box_width, box_height, width, height)
{
var new_width = width
var new_height = height
var aspect_ratio = parseInt(width) / parseInt(height)
if (new_width > box_width)
{
new_width = box_width
new_height = int(new_width / aspect_ratio)
}
if (new_height > box_height)
{
new_height = box_height
new_width = int(new_height * aspect_ratio)
}
return (new_width, new_height)
}
如您所见,我正在尝试输入一些值并返回 new_width 和 new_height。
感谢您的帮助!
最佳答案
如果您想一次返回两个值,您可以使用对象文字来完成...
return {
width: new_width,
height: new_height
};
关于javascript - 在 jQuery 中使用 JavaScript 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4811696/