Javascript 为什么可以';我不能连接这些JS函数吗?

Javascript 为什么可以';我不能连接这些JS函数吗?,javascript,input,syntax,Javascript,Input,Syntax,如果在onclick中的函数中,我说: offset=parseFloat(document.getElementById("offsetId").value); //value=-2 center=sp+offset; // sp is a float == 190 alert("center="+center); 我得到“center=190[object HTMLInputElement]” 如果我将功能分离为: offset=document.getElementById("offse

如果在onclick中的函数中,我说:

offset=parseFloat(document.getElementById("offsetId").value); //value=-2
center=sp+offset; // sp is a float == 190
alert("center="+center);
我得到“center=190[object HTMLInputElement]”

如果我将功能分离为:

offset=document.getElementById("offsetId"); 
offset=parseFloat(offset.value);
center=sp+offset;
alert("center="+center);
我也犯了同样的错误

但是如果我这样做的话

offset=document.getElementById("offsetId"); 
center=sp+parseFloat(offset.value);
alert("center="+center);
我得到了正确的答案,中心=188

为什么我不能像前两个例子那样连接函数? 对我来说,他们似乎一模一样

编辑: 下面是产生“不可能”结果的示例代码:


新文件
sp=parseFloat(190.);
输入偏移量

请注意,最后一种方法给出了正确答案。
我怀疑我无法让sp显示一个类型可能是重要的。

您尚未声明变量,因此
offset
不是您所认为的

在事件处理程序的上下文中,
offset
是对表单已定义的
的引用

如果要使用同名的局部变量对现有变量进行阴影处理,则需要使用
var offset
声明它

sp=parseFloat(190.);
输入偏移量


或示例都不会产生您声称的输出。
parseFloat
不可能返回HTML元素、表示HTML元素的字符串或任何其他会产生问题输出的值。请包括一个复制您的问题的工作样本。使用内置的代码编辑器。我举了一个例子(现在是一个完整的程序),它给出的输出非常不寻常,以至于您声称这是不可能的,您认为它不值得发布吗?请查看我发布的程序以及“不可能”警报输出。您有责任提供重现问题的代码,但您没有做到这一点。既然您已经生成了重现问题的代码,那么问题和解决方案就显而易见了。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
</head>

<body>
<script>sp=parseFloat(190.); </script>
<!-- same result with sp=190.;  sp=Number(190.); sp=Number("190."); -->

<form>
    <p>Enter Offset   <input id="offsetId" type="text" name="offset" value=0> <!-- eg -2 -->
    <p> 
    <input 
    type="button" 
    name="go"
    value="Do the Plot"
        onclick='
            alert("at19: sp = "+sp+", typeof sp=", typeof sp);  
                            // shows: at19: sp = 190, typeof sp=
            offset=parseFloat(document.getElementById("offsetId").value); 
            alert("at22, offset="+offset+", typeof offset="+typeof offset);
    // shows: at23, offset=[object HTMLInputElement], typeof offset=object
            center=sp+offset;
            alert("at25: center="+center);
            // shows: at24: center=190[object HTMLInputElement]

// alternate 1
            offset = parseFloat(offset.value);
            center=sp+offset;
            alert("at31: offset="+offset+", center="+center);
// shows: at31: offset=[object HTMLInputElement], center=190[object HTMLInputElement]

// alternate 2
            offset=document.getElementById("offsetId"); 
            center=sp+parseFloat(offset.value);
            alert("at37: offset="+offset+", center="+center);
// shows: at36: offset=[object HTMLInputElement], center=188
        '
        >
    </form>     
 </body>
</html>