不循环遍历所有数据

时间:2017-04-03 05:12:45

标签: javascript jquery

我创建了一个脚本来获取html元素中的数据,有一个带有id代码的div,我想获取ticker div中存在的div中的所有数据我只得到我控制台日志中的第一个没有得到其他人,请任何人帮助我

$(document).ready(function() {
var table = $("#ticker");
var calls = [];
table.each(function (i) {
    var $tds = $(this).find('div'),
    url = $tds.eq(0).text(),
    Company_name = $tds.eq(1).text(),
    open = $tds.eq(2).text();
    high = $tds.eq(3).text();
    var row_id = (i + 1);   
    console.log(url);
}); 
});

这是我的HTML

<div id="ticker" style="height:18px;font-family:Arial;font-size:12px;font-weight:bold;padding-top:3px;width:6000px;">


<div style="width:180px;float:left;text-align:center;">
    <font color="#f6c644">tag 1</font>
    &nbsp;&nbsp;52.24&nbsp;/&nbsp;1.33M&nbsp;
    <font color="#4eff00">&nbsp;0.16</font>
    </div> 
    <div style="width:180px;float:left;text-align:center;">
    <font color="#f6c644">tag 2</font>&nbsp;&nbsp;93.5&nbsp;/&nbsp;1.07M&nbsp;
    <font color="#4eff00">&nbsp;1.4</font>
    </div> 
    <div style="width:180px;float:left;text-align:center;">
    <font color="#f6c644">tag 3</font>&nbsp;&nbsp;74.2&nbsp;/&nbsp;0.84M&nbsp;
    <font color="#ff0000">&nbsp;-0.47</font>
    </div>
</div>  

2 个答案:

答案 0 :(得分:2)

var table = $("#ticker");
var calls = [];
table.find('div').each(function(i,item) {
    var $tds = $(this);
    var url = $tds.eq(0).text();
    var Company_name = $tds.eq(1).text();
    var open = $tds.eq(2).text();
    var high = $tds.eq(3).text();
    var row_id = (i + 1);   
    console.log(url);
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ticker" style="height:18px;font-family:Arial;font-size:12px;font-weight:bold;padding-top:3px;width:6000px;">


<div style="width:180px;float:left;text-align:center;">
    <font color="#f6c644">tag 1</font>
    &nbsp;&nbsp;52.24&nbsp;/&nbsp;1.33M&nbsp;
    <font color="#4eff00">&nbsp;0.16</font>
    </div> 
    <div style="width:180px;float:left;text-align:center;">
    <font color="#f6c644">tag 2</font>&nbsp;&nbsp;93.5&nbsp;/&nbsp;1.07M&nbsp;
    <font color="#4eff00">&nbsp;1.4</font>
    </div> 
    <div style="width:180px;float:left;text-align:center;">
    <font color="#f6c644">tag 3</font>&nbsp;&nbsp;74.2&nbsp;/&nbsp;0.84M&nbsp;
    <font color="#ff0000">&nbsp;-0.47</font>
    </div>
</div>

答案 1 :(得分:0)

将选择器从$("#ticker")更改为$("#ticker>div")

&#13;
&#13;
$(document).ready(function() {
    var table = $("#ticker>div");
    var calls = [];
    table.each(function (i) {
        var $tds = $(this);
        var url = $tds.text(),
            Company_name = $tds.eq(1).text(),
            open = $tds.eq(2).text();
            high = $tds.eq(3).text();
            var row_id = (i + 1);   
            console.log(url);
    }); 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ticker" style="height:18px;font-family:Arial;font-size:12px;font-weight:bold;padding-top:3px;width:6000px;">
    <div style="width:180px;float:left;text-align:center;">
        <font color="#f6c644">tag 1</font>
        &nbsp;&nbsp;52.24&nbsp;/&nbsp;1.33M&nbsp;
        <font color="#4eff00">&nbsp;0.16</font>
        </div> 
        <div style="width:180px;float:left;text-align:center;">
        <font color="#f6c644">tag 2</font>&nbsp;&nbsp;93.5&nbsp;/&nbsp;1.07M&nbsp;
        <font color="#4eff00">&nbsp;1.4</font>
        </div> 
        <div style="width:180px;float:left;text-align:center;">
        <font color="#f6c644">tag 3</font>&nbsp;&nbsp;74.2&nbsp;/&nbsp;0.84M&nbsp;
        <font color="#ff0000">&nbsp;-0.47</font>
    </div>
</div>
&#13;
&#13;
&#13;