Javascript:在div中选择一个图像元素 [英] Javascript: select an image element inside a div

查看:52
本文介绍了Javascript:在div中选择一个图像元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父div gal1 ,里面可以有更多的div和内容,但只有一个 img 元素没有id ,如下所示。现在我想只使用Javascript(没有jQuery )来改变这个图像的样式,使用 gal1 作为参数(因为结构的其余部分)在这个div里面可能会有所不同,只有这个一个图像总会出现在某处。我找不到其他任何能够解决我的情况的stackoverflow问题。

I have a parent div gal1, inside which there can be further divs and content, but only one img element without an id as you can see below. Now I want to use only Javascript (no jQuery) to change the style of this image, using gal1 as argument (because the rest of the structure inside this div may vary, only this one image will always be there somewhere). I couldn't find any other stackoverflow question that addresses exactly my situation.

<div class="gallery-preview" id="gal1">
    <div class="gallery-preview-img" id="gal-img1">
        <img src="galleries/kevin-carls/Monks.jpg">
    </div>
    <div class="gallery-preview-text" id="gal-text1">
        <span class="gallery-name" href="">Road to India</span><br/>
        <span class="artist-name" href="">Kevin Carls</span>
    </div>
</div>


推荐答案

您可以使用名为<$ c的方法$ c> getElementsByTagName('img')比你应该得到图片并更新它。

Than you can make use of method called getElementsByTagName('img') than you should get image and update it.

document.getElementById(gal1).getElementsByTagName('img');

这篇关于Javascript:在div中选择一个图像元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆