尝试使用JavaScript定位表单
我正在尝试使用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
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属性。