【问题标题】:Jquery to find replace text on json feedJquery在json feed上查找替换文本
【发布时间】:2015-11-20 11:44:07
【问题描述】:

我有以下 json 提要,如下所示循环以应用一些 css 样式:

        import({
           "Results":{
              "cars":[
                 {
                    "__":1010,
                    "_":"<div class=\"clearfix\"><img class=\"feed\" src=\"http://example.com/toyota.jpg\"/>
                            <p>Some text....</p>
                            <p><strong>$1000</strong></p>
                        </div>"
                 },
                 {
                    "__":2020,
                    "_":"<div class=\"clearfix\"><img class=\"feed\" src=\"http://example.com/nissan.jpg\"/>
                             <p>Some other text... </p>
                             <p><strong>$2000</strong></p>
                        </div>"
                 },

              ]
           }
        });


         for (var i = 0; i < data.Results["cars"].length; i++) {
              $("#results").append("<p >" + data.Results["cars"][i]["_"] + "</p>");

              $(".clearfix").css({float: "left"; width: "120px"; text-align:"center"; padding: "10px 0"});
         }

如何遍历每个元素并使用 jquery 提取图像名称(不包括扩展名)并将第一个 p 标记的文本替换为 跟随并将第二个p 标记替换为与图像名称具有相同值的单选按钮。

        <div class="clearfix" style="float:left;width:120px;text-align:center;padding:10px 0">
           <img src="http://example.com/toyota.jpg" >
           <p>Toyota</p>
           <p><input type="radio" value="Toyota" name="cartype"> </p>
        </div>

【问题讨论】:

    标签: jquery html css json


    【解决方案1】:
    // Iterating for every element in cars array
    [].forEach.call(data.Results.cars, function(inst){
    
        // Fetching the value of key _
        var _html = inst["_"];
    
        // Fetching the src of image by making string a jquery object
        var _src = $(_html).find("img").attr("src");
    
        // Building html
        var _h = '<div class="clearfix" style="float:left;width:120px;text-align:center;padding:10px 0">'
                    +'<img src="'+_src+'" >'
                    +'<p>'+_scr.split(".")[0]+'</p>'
                    +'<p><input type="radio" value="'+_scr.split(".")[0]+'" name="cartype"> </p>'
                 +'</div>';
    
        // Appending html to #results
        $("#results").append(_h);
    
    });
    

    【讨论】:

    • void: _scr.split(".")[0] 会给http://example.com/toyota 而不是toyota 不是吗?
    【解决方案2】:

    我尝试编辑之前的代码,但没有申请。所以我发布了这个答案。 我认为它对你有用。

    [].forEach.call(data.Results.cars, function(inst){
    
        // Fetching the value of key _
        var _html = inst["_"];
    
        // Fetching the src of image by making string a jquery object
        var _src = $(_html).find("img").attr("src");
    
        //Extracting the image's name
        var _imagesName = _scr.split("/")[_scr.split("/").length-1].split(".")[0];
        // Building html
        var _h = '<div class="clearfix" style="float:left;width:120px;text-align:center;padding:10px 0">'
                    +'<img src="'+_src+'" >'
                    +'<p>'+_imagesName+'</p>'
                    +'<p><input type="radio" value="'+imagesName'" name="cartype"> </p>'
                 +'</div>';
    
        // Appending html to #results
        $("#results").append(_h);
    
    });
    

    【讨论】:

    • [].forEach.call 在 IE8 中不起作用,因为它不支持 array.prototype.foreach?
    猜你喜欢
    • 2021-10-25
    • 2020-08-23
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    相关资源
    最近更新 更多