t.replace不是函数(...)修剪 [英] t.replace is not a function(…)trim

查看:214
本文介绍了t.replace不是函数(...)修剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在check.log中检查时产生的错误,并且它会破坏它

  var map = L.map('map')。setView([0,0],2); 

<?php
$ classesForCountries = [];
if(have_posts()):while(have_posts()):the_post();
$ classesForCountries [get_field('country')] + = get_field('year')+'';
endwhile;万一;
?>

//现在看起来应该像{Australia:2006 2010}

var classNameMap =<?php echo JSON_encode($ classesForCountries); ?取代;
geojson = L.geoJson(statesData,{
style:function(feature){

//这是我们得到问题的地方

var classes = classNameMap [feature.properties.sovereignt];
if(classes){
return {className:classes};
}
},
})。addTo (地图);

更新



<通过查看console.log,它指向我在库中的一行:

  ... t.replace( / ^ \s + | \s + $ / g,)},splitWords:function(t){return o.Util.trim(t).split(/ \s + /)} ... 


解决方案


@ Fred-非常好。非常感谢。把它放在答案中,我会接受它。 - rob.m


根据OP的要求:



+ = get_field('年')+''



您可能来自JS / C背景,并认为 + 符号可用于PHP中的连接。 PHP正在考虑你想要做数学的加号。



它是用PHP连接的点:

 。= get_field('year')。 ''


The following is producing the error in the question when I check in console.log and it breaks it

var map = L.map('map').setView([0, 0], 2);

<?php 
    $classesForCountries = [];
    if (have_posts()) : while (have_posts()) : the_post();
        $classesForCountries[ get_field('country') ] += get_field('year') + ' ';
    endwhile; endif;
?>

// Now this should look something like {"Australia": "2006 2010 "}

var classNameMap = <?php echo JSON_encode($classesForCountries); ?>;
geojson = L.geoJson(statesData, {
    style: function(feature) {

        // Here is where we got the issue

        var classes = classNameMap[feature.properties.sovereignt];
        if (classes) {
            return {className: classes};
        }
    },
}).addTo(map);

UPDATE

by looking at console.log, it points me to a line in the library:

...t.replace(/^\s+|\s+$/g,"")},splitWords:function(t){return o.Util.trim(t).split(/\s+/)}...

解决方案

"@Fred-ii- lol superb. Thanks a lot. Put that in an answer and I will accept it. – rob.m"

As per OP's request:

This += get_field('year') + ' '

You may have come from a JS/C background and think that the + signs can be used for concatenation in PHP. PHP is interpreting the plus signs in thinking you want to do math.

It is the dots that concatenate in PHP:

.= get_field('year') . ' '

这篇关于t.replace不是函数(...)修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆