jQuery 循环遍历对象并查找标题但跳过其他标题

发布于 2024-11-04 02:34:18 字数 2077 浏览 2 评论 0原文

我一直在自学 JS 和 Jquery,但我仍然在努力解决如何有效地使用 $ 循环对象并提取我想要的信息。

我有一个表格,其中有一个标志图标和一个描述我想放在行末尾的标题。然而,有时我必须忽略其他图标。因此,我在 url 中查找“标志”,如果存在,我将第二个到最后一个单元格的 innerHTML 设置为“标题”。

正如您在下面看到的,我必须创建一个递减 VAR y,以防止在遇到非标志图标时跳到带有“标题”的下一行。

我希望这一行附加“标题”,即位置:

<td>11:44pm</td><td><a href="/stats/visitors?site_id=66351439&amp;date=2011-04-27&amp;country=the+united+states"></a></td><td> <a class="custom" title="Comcast Cable" href="/stats/visitors?site_id=66351439&amp;date=2011-04-27&amp;ip_address=75.70.103.23">Comcast Cable</a></td><td><a href="/stats/visitors-actions?site_id=66351439&amp;date=2011-04-27&amp;session_id=228613269">1 action</a></td><td>10s</td><td width="100%"> &nbsp; </td><td></td>

看起来像这样:

<td>11:44pm</td><td><a href="/stats/visitors?site_id=66351439&amp;date=2011-04-27&amp;country=the+united+states"></a></td><td> <a class="custom" title="Comcast Cable" href="/stats/visitors?site_id=66351439&amp;date=2011-04-27&amp;ip_address=75.70.103.23">Comcast Cable</a></td><td><a href="/stats/visitors-actions?site_id=66351439&amp;date=2011-04-27&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朕就是辣么酷 2024-11-11 02:34:18

抱歉,但我不确定我是否太理解你的问题。但是,以下选择器将为您提供表中的所有标志

$('.tableborder tr img[src*="flag"]').each(function(){
   $(this)
    .closest('td')
    .nextAll('td:last')
    .html('Title from image : ' + $(this).attr('title'));
});

这应该让您有机会遍历 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

$('.tableborder tr img[src*="flag"]').each(function(){
   $(this)
    .closest('td')
    .nextAll('td:last')
    .html('Title from image : ' + $(this).attr('title'));
});

This should give you a chance to traverse the DOM and get at what you need

Live demo : http://jsfiddle.net/jomanlk/ukJxH/87/

热情消退 2024-11-11 02:34:18

您可以尝试使用 each

例如:

$(".tableborder tr img").each(function() {
   var src = $(this).attr("src");
   var alt = $(this).attr("alt");
});

You can try to use each

For example:

$(".tableborder tr img").each(function() {
   var src = $(this).attr("src");
   var alt = $(this).attr("alt");
});
南汐寒笙箫 2024-11-11 02:34:18

您还可以使用 css 类仅选择您感兴趣的 img 标签:

$(".tableborder tr img.flagImg").each(function() {
   ...
});

然后 HTML 将是

<img title="Arvada, CO, USA" class="flagImg" src="http://static.getclicky.com/media/flags/us.gif">

You can also use a css class to select only the img Tags you're interested in:

$(".tableborder tr img.flagImg").each(function() {
   ...
});

HTML would then be

<img title="Arvada, CO, USA" class="flagImg" src="http://static.getclicky.com/media/flags/us.gif">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文