Classname First Child src的第一个孩子

时间:2015-10-25 11:34:18

标签: jquery

我有以下要素:

<div class="house-box"><img class="Dicon" src="101"/></div>

<div class="house-box"><img class="Dicon" src="102"/></div>

<div class="house-box"><img class="Dicon" src="103"/></div>

我想检索一个属于Dicon的孩子house-box的元素,但我只想要第一个house-box

这是我到目前为止所拥有的:

function SelectCheck()
{
        var select_val = $('#companyCheck').val();
        var firstChildContent = $('.house-box');
        alert(firstChildContent.find('.Dicon:first').attr('src'));
        return;
}

我想要第一个src的{​​{1}}元素的house-box属性...

2 个答案:

答案 0 :(得分:0)

试试这个:让第一个孩子得到它属性类:你当前的代码给出了第一个src,即101.

&#13;
&#13;
function SelectCheck()
{
        var select_val = $('#companyCheck').val();
        var firstChildContent = $('.house-box');
        alert($(firstChildContent.children()[0]).attr("class"));//Dicon
       alert($(firstChildContent.children()[0]).attr("src"));//101
        return;
}

$(document).ready(function(){
	SelectCheck();
	});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="house-box"><img class="Dicon" src="101"/></div>
<!-- need to get the src of this one -->

<div class="house-box"><img class="Dicon" src="102"/></div>
<!-- not this one -->

<div class="house-box"><img class="Dicon" src="103"/></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以使用

 var firstChildSrc = $('.house-box:first-child').find('.Dicon').attr('src');

var firstChildSrc = $('.house-box:first').find('.Dicon').attr('src');

 var firstChildSrc = $('.house-box').first().find('.Dicon').attr('src');

DEMO