zoukankan      html  css  js  c++  java
  • JS 根据特定URL获取ID数组

    工作中遇到的问题 咱是菜鸟  有更好的方法 求大神看到的指点

    事情经过主要是后台返回商品ID 和 商品数量 然后做个卖光的遮罩 这样的效果  结果 后台返回的ID 数组不是后台输入的时候的排序 

    也就是说 到前台顺序不一样  我日

    为何不能是输入的一样的顺序 这个应该不难吧

    想了半天 突然看到商品的链接URL 是这样的 http://www.xxx.com/goods/27754.html 这个27754就是这个商品ID  幸好URL这个可以 要不然真没办法搞了

    HTML结构

    这样的

    <dl class="prod-box" >
                
                <dt class="item-prod-image">
                    <a href="http://www.xxx.com/goods/29225.html" target="_blank"><img width="180px" height="180" src="http://photo.xxx.com/product/mphoto/prod_29225_M_140911105842_23864418.jpg"></a>
                </dt>
                
                
                
                <dd class="item-prod-name">
                    <a href="http://www.xxx.com/goods/29225.html" target="_blank">Biotherm 碧欧泉活泉润透爽肤水(限用日期15年1月)&nbsp;30ml</a>
                </dd>
                
    
                
                <dd class="item-prod-price">
                    
                    <span class="price"><i></i>19</span>
                    <span class="shichang">¥200</span>
                   
                    
                </dd>
                
                
                
                <dd class="item-prod-button">
                    <button prod-id="29225" type="button" class="btn-add-to-cart"></button>
                </dd>
                
    
                
                
                
    
            
    </dl>

    这样就获取dl下面dt下的a 的href属性值就好了

    我JS是这样写的  JQ写的

    function getURL() {

    var href = [];
    $(".prod-box").each(function (i) {
    var a = $('.prod-box').eq(i).find('dt').find('a').attr('href');

    a = a.replace('http://www.xxx.com/goods/', '');
    a = a.replace('.html', '');
    href.push(a);

    })
    return href;
    }

    var id =getURL();

    这样倒是完成任务了 我觉得应该还有比这更方便的 比如 正则是不是更好 不过我不会 好伤心~

    要是有大神看到 求个指点 标记一下 省得以后忘记~

  • 相关阅读:
    校内题目T2695 桶哥的问题——吃桶
    一位大佬对学习哲理的思考
    P2845 [USACO15DEC]Switching on the Lights 开关灯
    CF911F Tree Destruction
    CF995C Leaving the Bar
    CF997B Roman Digits
    P1667 数列
    P4035 [JSOI2008]球形空间产生器
    P2679 子串
    P2613 【模板】有理数取余
  • 原文地址:https://www.cnblogs.com/xxx91hx/p/4053525.html
Copyright © 2011-2022 走看看