我创建了一个脚本来获取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>
52.24 / 1.33M
<font color="#4eff00"> 0.16</font>
</div>
<div style="width:180px;float:left;text-align:center;">
<font color="#f6c644">tag 2</font> 93.5 / 1.07M
<font color="#4eff00"> 1.4</font>
</div>
<div style="width:180px;float:left;text-align:center;">
<font color="#f6c644">tag 3</font> 74.2 / 0.84M
<font color="#ff0000"> -0.47</font>
</div>
</div>
答案 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>
52.24 / 1.33M
<font color="#4eff00"> 0.16</font>
</div>
<div style="width:180px;float:left;text-align:center;">
<font color="#f6c644">tag 2</font> 93.5 / 1.07M
<font color="#4eff00"> 1.4</font>
</div>
<div style="width:180px;float:left;text-align:center;">
<font color="#f6c644">tag 3</font> 74.2 / 0.84M
<font color="#ff0000"> -0.47</font>
</div>
</div>
答案 1 :(得分:0)
将选择器从$("#ticker")
更改为$("#ticker>div")
:
$(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>
52.24 / 1.33M
<font color="#4eff00"> 0.16</font>
</div>
<div style="width:180px;float:left;text-align:center;">
<font color="#f6c644">tag 2</font> 93.5 / 1.07M
<font color="#4eff00"> 1.4</font>
</div>
<div style="width:180px;float:left;text-align:center;">
<font color="#f6c644">tag 3</font> 74.2 / 0.84M
<font color="#ff0000"> -0.47</font>
</div>
</div>
&#13;