jQuery 循环遍历对象并查找标题但跳过其他标题
我一直在自学 JS 和 Jquery,但我仍然在努力解决如何有效地使用 $ 循环对象并提取我想要的信息。
我有一个表格,其中有一个标志图标和一个描述我想放在行末尾的标题。然而,有时我必须忽略其他图标。因此,我在 url 中查找“标志”,如果存在,我将第二个到最后一个单元格的 innerHTML 设置为“标题”。
正如您在下面看到的,我必须创建一个递减 VAR y,以防止在遇到非标志图标时跳到带有“标题”的下一行。
我希望这一行附加“标题”,即位置:
<td>11:44pm</td><td><a href="/stats/visitors?site_id=66351439&date=2011-04-27&country=the+united+states"></a></td><td> <a class="custom" title="Comcast Cable" href="/stats/visitors?site_id=66351439&date=2011-04-27&ip_address=75.70.103.23">Comcast Cable</a></td><td><a href="/stats/visitors-actions?site_id=66351439&date=2011-04-27&session_id=228613269">1 action</a></td><td>10s</td><td width="100%"> </td><td></td>
看起来像这样:
<td>11:44pm</td><td><a href="/stats/visitors?site_id=66351439&date=2011-04-27&country=the+united+states"></a></td><td> <a class="custom" title="Comcast Cable" href="/stats/visitors?site_id=66351439&date=2011-04-27&ip_address=75.70.103.23">Comcast Cable</a></td><td><a href="/stats/visitors-actions?site_id=66351439&date=2011-04-27&session_id=228613269">1 action</a></td><td>10s</td><td width="100%">Aurora, CO, USA</td><td></td>
所以我的问题 - 什么是完成整个过程的更好方法。这是 jsfiddle 的链接 - http://jsfiddle.net/ukJxH/78/ -在 3:47 您可以看到一个非旗帜图标。
var x = $('.tableborder tr img');
y = 1;
for (i = 0; i < x.length; i++) {
var flagg = x[i].src;
var pattn = /flag/gi;
var loca = x[i].title;
if (flagg.match(pattn) == "flag") {
//$('td:nth-child(6)')[i + 14 + y].innerHTML = loca;
$('#main :nth-child(i) td:nth-child(6)')[i].innerHTML = loc } else {
y = y - 1;
}
}
I have been teaching myself JS and Jquery and I still struggle with how to effectively use $ to loop through an object and extract the info I want.
I have a table that has a flag icon and a title describing that I want to put at end of the row. However, there are sometimes other icons that I must ignore. So I look for "flag" in url and if it exist I set innerHTML of 2nd to last cell to 'title'.
As you can see below I have had to create a decrement VAR y to keep from skipping to next row with "title" if I encounter non flag icon.
I want this row to be appended with "title" which is the location:
<td>11:44pm</td><td><a href="/stats/visitors?site_id=66351439&date=2011-04-27&country=the+united+states"></a></td><td> <a class="custom" title="Comcast Cable" href="/stats/visitors?site_id=66351439&date=2011-04-27&ip_address=75.70.103.23">Comcast Cable</a></td><td><a href="/stats/visitors-actions?site_id=66351439&date=2011-04-27&session_id=228613269">1 action</a></td><td>10s</td><td width="100%"> </td><td></td>
To look like this:
<td>11:44pm</td><td><a href="/stats/visitors?site_id=66351439&date=2011-04-27&country=the+united+states"></a></td><td> <a class="custom" title="Comcast Cable" href="/stats/visitors?site_id=66351439&date=2011-04-27&ip_address=75.70.103.23">Comcast Cable</a></td><td><a href="/stats/visitors-actions?site_id=66351439&date=2011-04-27&session_id=228613269">1 action</a></td><td>10s</td><td width="100%">Aurora, CO, USA</td><td></td>
So my question - What is a better way to do this whole procedure. Here is a link to the jsfiddle - http://jsfiddle.net/ukJxH/78/ - at 3:47 you can see a non flag icon.
var x = $('.tableborder tr img');
y = 1;
for (i = 0; i < x.length; i++) {
var flagg = x[i].src;
var pattn = /flag/gi;
var loca = x[i].title;
if (flagg.match(pattn) == "flag") {
//$('td:nth-child(6)')[i + 14 + y].innerHTML = loca;
$('#main :nth-child(i) td:nth-child(6)')[i].innerHTML = loc } else {
y = y - 1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉,但我不确定我是否太理解你的问题。但是,以下选择器将为您提供表中的所有标志
这应该让您有机会遍历 DOM 并获得您需要的内容
实时演示:http://jsfiddle.net/jomanlk/ukJxH/87/
I'm sorry but I'm not sure I understand your question too well. However, the following selector will give you all the flags in the table
This should give you a chance to traverse the DOM and get at what you need
Live demo : http://jsfiddle.net/jomanlk/ukJxH/87/
您可以尝试使用 each
例如:
You can try to use each
For example:
您还可以使用 css 类仅选择您感兴趣的 img 标签:
然后 HTML 将是
You can also use a css class to select only the img Tags you're interested in:
HTML would then be