创建元素样式
我是 DOM javascript 的初学者,所以我有这样的问题。 我有这样的 html 表单=>
<div id="name_field" style="text-align: center;">
<form name="add" action="index.php" method="post">
Name <input type="text" name="name_" id="name_" size="15"> <a href="javascript:void(0)" class="some" onclick="addChild();">Add More</a><br>
<span id="a">here is that</span>
</form>
</div>
并且想要像看到的那样调用 addChild() 函数,它将在 div 中附加带有样式(css)内容的“input”元素。我有这样的javascript代码=>
function addChild()
{
var div = document.getElementById("name_field");
var el = document.createElement("input");
el.style.size = 10;
div.appendChild(el);
}
但它不起作用......还需要从上到下附加“输入”元素以及如何做到这一点,如果有人帮助我。谢谢 ... :)
I'm beginner in DOM javascript, So I have such a question.
I have html form like that =>
<div id="name_field" style="text-align: center;">
<form name="add" action="index.php" method="post">
Name <input type="text" name="name_" id="name_" size="15"> <a href="javascript:void(0)" class="some" onclick="addChild();">Add More</a><br>
<span id="a">here is that</span>
</form>
</div>
And want to call like it is seen addChild() function which will append "input" element in div with style (css) content. I have such a javascript code =>
function addChild()
{
var div = document.getElementById("name_field");
var el = document.createElement("input");
el.style.size = 10;
div.appendChild(el);
}
But it is not working ... Also need to append "input" element from above to below and how to do that , if anyone help me. Thanks ... :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
input
元素的size
不是样式属性。它是input
元素本身的属性。所以这个...
应该是这个...
我只是不知道你的意思“还需要从上到下附加“输入”元素...”。
The
size
of aninput
element isn't a style property. It's a property of theinput
element itself.So this...
should be this...
I just don't know what you mean by "Also need to append "input" element from above to below...".
没有样式.尺寸。也许您正在寻找 style.height 或 style.width?
There is no style.size. Maybe you are looking for style.height or style.width?
首先,款式没有尺寸。你可以设置高度和宽度。
ele.style.width ='10px';
但之后它也不能跨浏览器兼容,所以你可以使用
ele.setAttribute('style','width:10px;height:10px');
您使用的大小在 Firefox 和 IE 中会有所不同。 !!!
Firstly there is no size in the style. u can set height and width.
ele.style.width ='10px';
But after it also it is not cross browser competible so u can go with,
ele.setAttribute('style','width:10px;height:10px');
The size which you using will be vary in Firefox and IE. !!!