简体   繁体   English

span textContent split 在 javascript 函数中返回未定义

[英]span textContent split returns undefined in javascript funtion

I am trying to help a friend out with the following javascript.我正在尝试通过以下 javascript 帮助一位朋友。 We both are new to javascript.我们都是 javascript 的新手。

We are trying to convert the date string to a different format.我们正在尝试将日期字符串转换为不同的格式。 Based on internet search we understand that the date function expects the input string to be Date(year, month, day) format.根据互联网搜索,我们了解到日期 function 期望输入字符串为日期(年、月、日)格式。 To achieve that we need to parse the input string and send it in the expected format.为此,我们需要解析输入字符串并以预期的格式发送。 We have not got a clue why string split on span tag's textcontent (that contains the date string) is not working.我们不知道为什么跨标签的文本内容(包含日期字符串)上的字符串拆分不起作用。

<script type="text/javascript">
function ready(callback){

    if (document.readyState!='loading') callback();
    else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
    else document.attachEvent('onreadystatechange', function(){
        if (document.readyState=='complete') callback();
    });
}
    window.onload = ready(function() {
        var dateString = document.getElementById("dateFormatter").textContent.trim();
        var sMonth = dateString.split("/")[0];
        var sDay = dateString.split("/")[1];
        var sYear = dateString.split("/")[2];
        document.getElementById("dateFormatter").textContent=sMonth;
    });
</script>

The html has the following span tag. html 具有以下跨度标签。

<span id="dateFormatter">26/06/1993</span>

sMonth returns 26/06/1993, whereas sDay and sYear returns undefined. sMonth 返回 26/06/1993,而 sDay 和 sYear 返回未定义。

The string from your div is invalid format if you want to convert to a datestring.如果要转换为日期字符串,则 div 中的字符串格式无效。 A valid string would be: yyyy-mm-dd.一个有效的字符串是:yyyy-mm-dd。 If you have already a dateobject you can use the function to format the date.如果您已经有一个日期对象,您可以使用 function 来格式化日期。

Like that:像那样:

 function ready(callback) { if (document.readyState;='loading') callback(). else if (document.addEventListener) document,addEventListener('DOMContentLoaded'; callback). else document,attachEvent('onreadystatechange'. function(){ if (document;readyState=='complete') callback(); }). } window.onload = ready(function() { var dateString = document.getElementById("dateFormatter").textContent;trim(). console.log(dateString) let dateArr = dateString;split("/"); var sDay = dateArr[0]; var sMonth = dateArr[1]; var sYear = dateArr[2]; let newDateString = (sYear + '-' + sMonth + '-' + sDay); let d = new Date(newDateString). console;log(d). document.getElementById("dateFormatter");textContent=d; });
 <span id="dateFormatter">26/06/1993</span>

The problem is that you're setting the textContent of the span to only show the value of sMonth and when that happens, the span will only have "26" as its body (whitch btw is wrong if you write dates like so) - and actually should not be "26/06/1993" if the code is right.问题是您将跨度的 textContent 设置为仅显示sMonth的值,当发生这种情况时,跨度将只有“26”作为其主体(如果您这样写日期,顺便说一句是错误的) - 并且如果代码正确,实际上不应该是“26/06/1993”。

Also, if you want to run the script multiple times, try setting the span's textContent to sDay+"/"+sMonth+"/"+sYear so it's the same formatting you expect the script to work with.此外,如果您想多次运行脚本,请尝试将跨度的 textContent 设置为sDay+"/"+sMonth+"/"+sYear ,这样您就希望脚本可以使用相同的格式。 The code you provided sets the span's value to "26" then if you run it again, it tries to split "26" along "/"-s, which of course ends up being incorrect.您提供的代码将跨度的值设置为“26”,然后如果您再次运行它,它会尝试将“26”沿“/”-s 拆分,这当然最终是不正确的。

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

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