Jquery: $.each()返回值然后结束
Jquery: $.each() returns value then ends
我有jQuery代码来调整几个iframe基于他们在容器。当我在没有.each()
操作符的情况下这样做时,所有的iframe都使用相同的div来确定它们的大小。对于.each()
,操作员运行时发出警报,但不继续执行功能。
Codepen: https://codepen.io/bjackson2016/pen/oLQopy?editors=0010
$(document).ready(function(){
function resize() {
var elements = $('.resize');
var eachElement = $.makeArray(elements);
alert(eachElement);
$.each(eachElement, function(index, value) {
alert(index + " " + value);
var multi = value.data('wfactor');
alert(multi);
var ratio = value.data('ratio');
alert(ratio);
var minX = value.data('minw');
alert(minX);
var minY = value.data('minh');
alert(minY);
var ruler = value.siblings('.widthTest');
alert(ruler);
var pWidth = ruler.width();
alert(pWidth);
var adjWidth = pWidth * multi;
alert(adjWidth);
var eHeight = (Math.round(ratio * adjWidth));
alert(eHeight);
var unadjHeight = Math.round(ratio * pWidth);
alert(unadjHeight);
var eWidth = (Math.round(adjWidth));
alert(eWidth);
if (eHeight < minY) {
$(this).height(minY);
alert('changed height');
} else {
value.height(eHeight);
alert('normal height');
}
if (eWidth < minX) {
value.css('width', pWidth).css('height', unadjHeight);
alert('changed width');
} else {
value.width(eWidth);
alert('kept width');
}
});
}
resize();
$(window).resize(function() {
resize();
});
});
问题是没有value.data()
。
data()
是一个jQuery函数,并与$.each
迭代展开的元素,所以你试图调用data()
在一个本地DOM节点
$.each(eachElement, function(index, value) {
// value is not a jQuery object here, you'd have to do
var elem = $(value); // where you wrap it again.
});
尝试相反
$(document).ready(function() {
$(window).on('resize', resize).trigger('resize');
function resize() {
$('.resize').each(function(index, element) {
var elem = $(element);
var multi = elem.data('wfactor'),
ratio = elem.data('ratio'),
... etc
});
}
});
相关文章:
- Javascript返回值只在循环中返回一次
- XMLHttpRequest未返回值-状态202
- 根据是否解析了 Promise 从函数返回值
- Angular,函数在(模型)工厂中返回值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何从客户端的数组中获取用户输入和返回值
- Javascript中带有返回值的嵌套函数
- 为变量分配多个nodejs导出返回值时出现问题
- 通过回调从onreadystatechange返回值
- 从Ajax函数返回值
- JavaScript生成器中收益率返回值的解析
- HTML如何根据javascript函数的返回值限制文本输入
- 等待函数完成,然后将返回值分配给变量JS
- Javascript函数,它接受可变数量的参数 - 更改它们的值 - 然后返回它们
- 使用isNan()函数将参数从html传递给javascript,并检查值然后返回值
- 重复一个函数调用7次,然后使用angularjs用返回值填充标签
- 更改复选框时调用函数,获取返回值,然后提交表单
- 使用javascript返回值,然后将其添加到ruby数据库中
- Jquery: $.each()返回值然后结束
- 如何将字符串从javascript传递到服务器?然后从asp.net web表单中的服务器获取返回值