V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tt210
V2EX  ›  问与答

jquery map()方法 执行次数变少。

  •  
  •   tt210 · 2015-10-14 16:21:27 +08:00 · 1460 次点击
    这是一个创建于 3132 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 chrome 上写了个油猴脚本,用来转换目标网站的链接,用 jquery 获取的链接是 10 个,但是用 myurl.map()方法却只能执行 7 次,真是花了狗了,请求大神指点,谢谢。
    部分代码如下:

    var SourceUrls = $('#content_left .g'),
         Re = /(.*?)\//,
         c = 0 ;
    SourceUrls.map(function () {
        SourceUrl = (Re.exec($(this).text()))[1];
        c++;
        alert(c);
        alert(SourceUrl);
    });
    
    第 1 条附言  ·  2015-10-14 17:05:28 +08:00

    测试 demo

    // ==UserScript== 
    // @name My Fancy New Userscript 
    // @namespace http://your.homepage/ 
    // @version 0.1 
    // @description enter something useful 
    // @author You 
    // @grant none 
    // @match https://www.baidu.com/* 
    // @require http://www.w3school.com.cn/jquery/jquery.js 
    // ==/UserScript== 
    var SourceUrls = $('#content_left .g'), 
    SourceUrl, 
    Re = /(.*?)\//, 
    c = 0; 
    SourceUrls.map(function () { 
    SourceUrl = (Re.exec($(this).text()))[1]; 
    c++; 
    alert('执行次数:' + c + '\n' + 'SourceUrls 元素数量:' + SourceUrls.length); 
    }); 
    alert('执行次数:' + c + '\n' + 'SourceUrls 元素数量:' + SourceUrls.length); //这个不会执行,为什么?
    
    第 3 条附言  ·  2015-10-14 17:09:45 +08:00
    就这个关键字有问题,随便摘了个新闻标题,汗啊。其他的都可以。真是无语。
    第 4 条附言  ·  2015-10-14 17:18:54 +08:00
    明白问题所在了。正则表达式没有匹配成功,出错了,所以后面的没有执行。
    chairuosen
        1
    chairuosen  
       2015-10-14 16:33:30 +08:00
    need demo
    tt210
        2
    tt210  
    OP
       2015-10-14 16:45:44 +08:00
    我去,又正常了。虾米情况啊。我再测试下。谢谢楼上。
    tt210
        3
    tt210  
    OP
       2015-10-14 16:57:21 +08:00
    ```
    // ==UserScript==
    // @name My Fancy New Userscript
    // @namespace http://your.homepage/
    // @version 0.1
    // @description enter something useful
    // @author You
    // @grant none
    // @match https://www.baidu.com/*
    // @require http://www.w3school.com.cn/jquery/jquery.js
    // ==/UserScript==
    var SourceUrls = $('#content_left .g'),
    SourceUrl,
    Re = /(.*?)\//,
    c = 0;
    SourceUrls.map(function () {
    SourceUrl = (Re.exec($(this).text()))[1];
    c++;
    alert('执行次数:' + c + '\n' + 'SourceUrls 元素数量:' + SourceUrls.length);
    });
    alert('执行次数:' + c + '\n' + 'SourceUrls 元素数量:' + SourceUrls.length); //这个不会执行,为什么?
    tt210
        4
    tt210  
    OP
       2015-10-14 16:59:12 +08:00
    大家帮忙测试下,正常不?刚才测试还是有问题。好像加了那句正则匹配就出错。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   992 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:20 · PVG 04:20 · LAX 13:20 · JFK 16:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.