繁体   English   中英

而不是重复代码块,而是编写此javascript的更有效方法?

[英]Instead of repeating blocks of code, more efficient way to write this javascript?

在这里,我只是一遍又一遍地重复相同的代码,只是递增变量名。 与对RSS2JSON实用程序的feed URL调用相同。 我能以某种方式写这个,所以我不必重复吗? 我实际上有8种不同的供稿。

<script type="text/javascript">
        var cpacontent = document.getElementById('cpa');
        function showFeed(data){
            if(data.status == 'ok'){    
                var feedlength = data.items.length;
                var output = '';
                for(var i=0;i<1;++i){
                    output += '<p><a href="' +
                    data.items[i].link + '" target="_blank" >' +
                    data.items[i].title + '</a>';
                }
                cpacontent.innerHTML = output;
            }
        }

        var fscontent = document.getElementById('fs');
        function showFeed2(data){
            if(data.status == 'ok'){    
                var feedlength2 = data.items.length;
                var output2 = '';
                for(var i=0;i<1;++i){
                    output2 += '<p><a href="' +
                    data.items[i].link + '" target="_blank" >' +
                    data.items[i].title + '</a>';
                }
                fscontent.innerHTML = output2;
            }
        }
        var wealthcontent = document.getElementById('wealth');
        function showFeed3(data){
            if(data.status == 'ok'){    
                var feedlength3 = data.items.length;
                var output3 = '';
                for(var i=0;i<1;++i){
                    output3 += '<p><a href="' +
                    data.items[i].link + '" target="_blank" >' +
                    data.items[i].title + '</a>';
                }
                wealthcontent.innerHTML = output3;
            }
        }    
</script>
<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed1&rss_url=http%3A%2F%2Ffeedurlplaceholder1"></script>

<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed2&rss_url=http%3A%2F%2Ffeedurlplaceholder2"></script>

<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed3&rss_url=http%3A%2F%2Ffeedurlplaceholder3"></script>

向函数添加第二个参数以接受元素。

就像是:

<script>
function myFunction(selectorID, data) {
    var element = document.getElementById('selectorID');
    if(data.status == 'ok'){    
        var dataLength = data.items.length;
        var output = '';
        for(var i = 0; i < 1; ++i){
            output += '<p> <a href="' +
            data.items[i].link + '" target="_blank" >' +
            data.items[i].title + '</a>';
        }
        cpacontent.innerHTML = output;
    }
}

myFunction("cpa", cpaData);
myFunction("fs", fsData);
myFunction("wealth", wealthData);
</script>

希望你一切都好.. !!

您可以使用一个函数showFeed()和两个参数分别为datavarFlag ,如下所示:

<script type="text/javascript">
     function showFeed(data, varFlag){
         if(data.status == 'ok'){    
             var feedlength = data.items.length;

             var output = '';
             for(var i=0;i<1;++i){
                 output += '<p><a href="';
                 output += data.items[i].link + '" target="_blank" >';
                 output += data.items[i].title + '</a>';
             }

             if(varFlag == 'cpa') { cpacontent.innerHTML = output; }

             if(varFlag == 'fs') { fscontent.innerHTML = output; }

             if(varFlag == 'wealth') { wealthcontent.innerHTML = output; }
         }
     }

     var cpacontent = document.getElementById('cpa');
     showFeed(data, 'cpa');

     var fscontent = document.getElementById('fs');
     showFeed(data, 'fs');

     var wealthcontent = document.getElementById('wealth');     
     showFeed(data, 'wealth');    
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM