javascript - 如何改变div内所有图像的高度?

标签 javascript jquery

如何更改div内所有图像的高度?

我有

$('.images img').each(function() {
      $('img').attr('height', element.parent().attr('imgheight'));
    });

<div imgheight="300px" class="images">
  <img src=" http://www.yu51a5.com/wp-content/uploads/2015/01/goldenhorseman.jpg " />
  <img src=" http://www.yu51a5.com/wp-content/uploads/2015/01/bronze-horseman.jpg " />
</div>

它似乎没有做任何事情 - 请参阅 https://jsfiddle.net/yu51a5/527gn64n/4/ 。如何让它发挥作用?

我不能使用“height=100%”,因为通常这些图像带有标题,因此图像应该比 div 短。

最佳答案

您尚未定义元素。从每个函数内部访问节点。

$('.images img').each(function(i, node) {
  $('img').attr('height', $(node).parent().attr('imgheight'));
});

Working fiddle.

关于javascript - 如何改变div内所有图像的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36296622/

相关文章:

javascript - jQuery POST 在某些浏览器上不起作用

javascript - 循环遍历 slider

javascript - 如何通过ajax在Js图表中设置值?

javascript - 列表中的元素高度相等

javascript - 如何从 Bootstrap 复选框按钮组中获取值?

javascript - 表单提交必填空字段显示消息 "Please fill out this field."

javascript - 在 js 中使用 ajax 的联系表单不会 POST

javascript - NodeJS 函数在 ajax 调用上不返回响应

javascript - 从远程站点加载时未检测到 vis.js 中的语法错误。使用本地副本检测到

javascript - 关于Jquery库的一些问题