-1
我已经使这个函数从字符串中存在的单词数中获取价值,并将其从自定义价格中乘以它。这两个值都来自输入字段。然而,它通过调用这个函数“NaN”来给我一个错误。你能在这里发现问题吗?无法读取空值返回的属性'分割'NaN
<!DOCTYPE html>
<html>
\t <head>
\t \t <title>Telegramma</title>
\t \t <script>
\t \t var string = document.getElementById("text");
\t \t var words = new Number (string.split(" ").length);
\t \t var price = new Number (document.getElementById("price").value);
\t \t function showPrice(){
\t \t var total = new Number (words * price);
\t \t \t document.getElementById("showPrice").innerHTML = total;
}
\t \t </script>
\t </head>
\t </body>
</html>
<body>
Text:<input id = "text" type = "text"/>
Price per a word €<input id = "price" min="0.01" step="0.01" type = "number" />
<button onclick = "showPrice()">Get the full price</button>
<p id = "showPrice"> </p>
把'script'标签body'标签后'。 – gurvinder372
脚本出现的地方不如代码正确。 'string','words'和'price'的声明和初始化应该在**函数内部,而不是在外部。随着这一变化,'
那是因为当你的脚本执行,在不创建的内容,但。将所有内容放在
body
关闭标记之前的所有内容之后。或者在身体负载事件上执行所有事情来源
2017-04-09 15:00:51 ghostprgmr