jQuery jquery拆分函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5089015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:33:09  来源:igfitidea点击:

Jquery split function

jquery

提问by oldrock

I have a ajax function as below

我有一个 ajax 函数,如下所示

<script type="text/javascript">
$(document).ready(function () {

var timer, delay =600000; //5 minutes counted in milliseconds.

timer = setInterval(function(){
    $.ajax({
  type: 'POST',
  url: 'http://localhost/cricruns-new/index.php/score-refresh/fetchdes/index/86/1',
  success: function(html){

    alert(html);
  }
});

},delay);

});
</script>

it outputs the following

它输出以下内容

total:-370:-wickets:-4:-overs:-50.0:-striker:-Yusuf Pathan:-sruns:-8:-sballs:-10:-sfour:-0:-ssix:-0:-nonstriker:-Virat Kohli:-nsruns:-100:-nsballs:-83:-nsfours:-8:-nssix:-2

total:-370:-wickets:-4:-overs:-50.0:-striker:-Yusuf Pathan:-sruns:-8:-sballs:-10:-sfour:-0:-ssix:-0:-nonstriker:-Virat Kohli:-nsruns:-100:-nsballs:-83:-nsfours:-8:-nssix:-2

i want to split the result using :-and i have to assign even values to the div which has the id as the odd value..

我想使用拆分结果:-,我必须将偶数值分配给 id 作为奇数值的 div..

回答by nickf

Try this. It uses the splitfunction which is a core part of javascript, nothing to do with jQuery.

尝试这个。它使用的split是javascript核心部分的函数,与jQuery无关。

var parts = html.split(":-"),
    i, l
;
for (i = 0, l = parts.length; i < l; i += 2) {
    $("#" + parts[i]).text(parts[i + 1]);
}

回答by Ben Griffiths

Javascript String objects have a split function, doesn't really need to be jQuery specific

Javascript String 对象具有拆分功能,实际上并不需要特定于 jQuery

 var str = "nice.test"
 var strs = str.split(".")

strs would be

strs 将是

 ["nice", "test"]

I'd be tempted to use JSON in your example though. The php could return the JSON which could easily be parsed

不过,我很想在您的示例中使用 JSON。php 可以返回可以轻松解析的 JSON

 success: function(data) {
   var items = JSON.parse(data)
 }