Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用JavaScript定位表单_Javascript_Html_Dom - Fatal编程技术网

尝试使用JavaScript定位表单

尝试使用JavaScript定位表单,javascript,html,dom,Javascript,Html,Dom,我正在尝试使用JavaScript创建一个表单并定位它。我使用以下代码 var f = document.createElement("form"); f.setAttribute('method',"post"); f.setAttribute('action',"submit.php"); // Position form f.setAttribute('offsetTop',offsetTop); f.setAttribute('offsetLeft',parseInt(offsetLe

我正在尝试使用JavaScript创建一个表单并定位它。我使用以下代码

var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

// Position form
f.setAttribute('offsetTop',offsetTop);
f.setAttribute('offsetLeft',parseInt(offsetLeft)+parseInt(imageWidth));

document.getElementsByTagName('body')[0].appendChild(f);
但是,当我使用Firebug检查DOM时,表单的位置与我根本没有使用表单定位调用的位置相同。也就是说,它位于图像的最后一个元素下面。

试试这个

var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

// Position form
f.style.position = "absolute";
f.style.top = offsetTop +"px";
f.style.left = (parseInt(offsetLeft)+parseInt(imageWidth)) +"px";
试试这个

var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

// Position form
f.style.position = "absolute";
f.style.top = offsetTop +"px";
f.style.left = (parseInt(offsetLeft)+parseInt(imageWidth)) +"px";

offsetTop和offsetLeft不是CSS属性。你需要使用的不是静态位置。使用“样式”:


offsetTop和offsetLeft不是CSS属性。你需要使用的不是静态位置。使用“样式”:


你需要使用CSS布局技术。为什么CSS比JavaScript更可取。是否因为JavaScript可以被禁用,有时甚至被禁用?谢谢,彼得,不用了。Javascript无法定位元素。Javascript可以设置定位元素的CSS属性。您需要使用CSS布局技术。为什么CSS比Javascript更可取。是否因为JavaScript可以被禁用,有时甚至被禁用?谢谢,彼得,不用了。Javascript无法定位元素。Javascript可以设置定位元素的CSS属性。