【问题标题】:t.replace is not a function(…)trimt.replace 不是函数(…)修剪
【发布时间】:2016-11-03 11:10:37
【问题描述】:

当我签入 console.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; 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);

更新

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

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

【问题讨论】:

  • 在您发布的代码中的任何地方都看不到t.replace
  • @void 恐怕就是控制台所说的,不知道为什么,除了你在上面看到的以外,别无他法
  • 控制台还应该指明一个行号,你应该能够与代码中的一行匹配,或者它可能在包含的脚本/库中
  • @trincot 它确实来自库,控制台说 leaflet.js:5 Uncaught TypeError: t.replace is not a function(...)
  • += get_field('year') + ' ' 你想在这里做数学吗?如果没有,你在这里是 php,而不是 js 连接。所以使用点而不是加号。 .= get_field('year') . ' '

标签: javascript php jquery json leaflet


【解决方案1】:

“@Fred-ii-lol 太棒了。非常感谢。把它放在一个答案中,我会接受它。-rob.m”

根据 OP 的要求:

这个+= get_field('year') + ' '

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

它是 PHP 中连接的点:

.= get_field('year') . ' '

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2012-05-23
    • 2017-02-21
    • 2014-01-22
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    相关资源
    最近更新 更多