无法获得跨度值

时间:2016-08-18 17:43:06

标签: javascript jquery google-maps

我是Javascript和Jquery的新手,刚刚开始学习,我从其他网站复制代码并做了一些细微的更改,我在点击图标时尝试更新计时器,但是我收到了这个错误: - Uncaught TypeError: Cannot read property 'split' of undefined

找到后,我看到变量myTime未定义。它没有得到跨度值。这是我的代码: -

    function updateTimer(expiryid) {
            var myTime = $(expiryid+ ' span').html();
            console.log(myTime);
            var ss = myTime.split(":");
            var dt = new Date();
            dt.setHours(0);
            dt.setMinutes(ss[0]);
            dt.setSeconds(ss[1]);

            var dt2 = new Date(dt.valueOf() + 1000);
            var temp = dt2.toTimeString().split(" ");
            var ts = temp[0].split(":");

            expiryid.html(ts[1]+":"+ts[2]);
        }


     function initialize() {
     // Some un-related code up here

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infoWindow.setContent(infoWindowContent[i][0]);
                        infoWindow.open(map, marker);
                        //console.log('expire_'+i);
                        updateTimer('#expire_'+i);
                    }
                })(marker, i));

    }

</script>

    <span id="expire_4">06:51</span>

1 个答案:

答案 0 :(得分:1)

您的html为CalculateToMax但您正在尝试查询<span id="expire_4">06:51</span> var myTime = $(expiryid+ ' span').html();。问题是您的查询与您的代码不匹配。您的查询表明您希望存在ID为expiryid = '#expire_'+i且具有子跨度的元素。你拥有的是一个标识为expire_<number>的范围。更新查询以将span元素设为expire_<number>,您应该好好去。

编辑我还注意到您正在尝试更新变量的html。将整个功能更新为:

$(expiryid)
相关问题