jquery怎么选择某个div下的某一个元素

如题所述

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html,并引入jquery,编写问题基础代码。

2、在index.html中的<script>标签,输入jquery代码:

$('#a').html('<img src="' + $('#img1 img').attr('src') + '"/>');

3、浏览器运行index.html页面,此时成功选择到img1中的图片并显示在另一个div中。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-03
为div添加id 或者class,我以id为例子。

布局样式:<div id="div的Id"> <span>文字内容</span><span></span> </div>

目标:获取div中的第二个span

脚本:$("#div的Id span:eq(1)").事件(funciton(){事假触发后执行的代码});

效果:id为“div的Id”的元素中第二个span被选中。本回答被网友采纳
第2个回答  2019-10-21
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div class="parent">
        <div class="children">0</div>
        <div class="children">1</div>
        <div class="children">2</div>
        <div class="children">3</div>
        <div class="children">4</div>
    </div>
</body>
<script>
    alert(document.querySelectorAll(".parent .children")[3].innerHTML)
</script>

</html>

请采纳