获取 img 的宽度和高度,并使用 javascript 将其添加到最近的父级

发布于 2024-12-28 07:53:20 字数 533 浏览 2 评论 0原文

我已经弄清楚如何使用 javascript 获取图像的宽度和高度。我的问题是如何将宽度和高度应用到它最近的父级,在这种情况下它是使用 javascript 的 li。

<ul>
    <li> <img src="img1.jpg" /> </li>
    <li> <img src="img2.jpg" /> </li>
</ul>

我正在使用的js:

$(window).load(function() {
    var pic = $('img');
    var w = pic.width();
    var h = pic.height();

    pic.removeAttr("width"); 
    pic.removeAttr("height");

    $('ul li').css('width',w);
});

现在它只是获取第一个图像的宽度和高度并将其应用到每个li。

I've figured out how to get the width and hight of an image using javascript. My question is how do I apply the width and height to it's nearest parent in this case it's li using javascript.

<ul>
    <li> <img src="img1.jpg" /> </li>
    <li> <img src="img2.jpg" /> </li>
</ul>

The js i'm using:

$(window).load(function() {
    var pic = $('img');
    var w = pic.width();
    var h = pic.height();

    pic.removeAttr("width"); 
    pic.removeAttr("height");

    $('ul li').css('width',w);
});

Right now it is only getting the width and height of the first image and applying it to every li.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

满天都是小星星 2025-01-04 07:53:21

下面的解决方案已被修改以证明其有效性:

CSS:

li {
  border: 1px solid #000; 
  width: 80px;
  height: 100px;
}

HTML:

<ul>
    <li id="one"> <img src="img1.jpg" width="50" height="80" /> </li>
    <li id="two"> <img src="img2.jpg" width="70" height="50" /> </li>
</ul>
<input type="button" value="Change" id="change">

jQuery:

$('#change').click(function(){
    $('img').each(function(){
        e = $(this);
        dim = [e.width(),e.height()];
        e.parent().css({width: dim[0], height: dim[1]});
    });
});

示例: http://jsfiddle.net/kfnhh /

只需删除点击事件处理程序,代码就可以在您的环境中轻松实现。

The solution below has been modified to demonstrate its effectiveness:

CSS:

li {
  border: 1px solid #000; 
  width: 80px;
  height: 100px;
}

HTML:

<ul>
    <li id="one"> <img src="img1.jpg" width="50" height="80" /> </li>
    <li id="two"> <img src="img2.jpg" width="70" height="50" /> </li>
</ul>
<input type="button" value="Change" id="change">

jQuery:

$('#change').click(function(){
    $('img').each(function(){
        e = $(this);
        dim = [e.width(),e.height()];
        e.parent().css({width: dim[0], height: dim[1]});
    });
});

Example: http://jsfiddle.net/kfnhh/

Simply remove the click event handler and the code should be easily implemented in your environment.

忆伤 2025-01-04 07:53:21

尝试类似的事情:

$('img').each(function () {

    var pic = $(this) ;
    var w = pic.width();
    var h = pic.height();

    pic.removeAttr("width"); 
    pic.removeAttr("height");

    pic.parent().css('width',w)
})

没有尝试,但想法就在那里。

Try something like :

$('img').each(function () {

    var pic = $(this) ;
    var w = pic.width();
    var h = pic.height();

    pic.removeAttr("width"); 
    pic.removeAttr("height");

    pic.parent().css('width',w)
})

Didn't try but the idea is there.

时光清浅 2025-01-04 07:53:21

另一种方法:

pic.closest("li").css({'width' : w , 'height' : h});

Another way to it:

pic.closest("li").css({'width' : w , 'height' : h});
寻找一个思念的角度 2025-01-04 07:53:21

.parent() 将为您提供您正在使用它的控件的父级。在您的情况下,以下内容应该为您提供图像控件的父控件。

// jQuery 
$(this).parent()

// Javascript
element.parentNode

.parent() will get you the parent of what ever control you are using it on. In your case the following should get you the parent control of your image control.

// jQuery 
$(this).parent()

// Javascript
element.parentNode
难理解 2025-01-04 07:53:21

尽可能少地改变:

$(window).load(function() {
    $('img').each(function() {
        var pic = $(this);
        var w = pic.width();
        var h = pic.height();

        pic.removeAttr("width"); 
        pic.removeAttr("height");

        pic.parents('li').css('width',w);
    });
});

这有效吗?

To change as little as possible:

$(window).load(function() {
    $('img').each(function() {
        var pic = $(this);
        var w = pic.width();
        var h = pic.height();

        pic.removeAttr("width"); 
        pic.removeAttr("height");

        pic.parents('li').css('width',w);
    });
});

Would that work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文