使用javascript动态创建div水平布局元素

时间:2015-01-27 20:46:48

标签: javascript html polymer

如何使用javascript动态创建<div horizontal layout></div>元素?

我可以用document.createElement(“div”)创建div元素;但是如何为其添加水平布局?

1 个答案:

答案 0 :(得分:3)

<html>
<head>

<script type="text/javascript">
window.onload = function() {

newdiv = document.createElement("div");
newdiv.setAttribute('horizontal', '');
newdiv.setAttribute('layout', '');
document.body.appendChild(newdiv);
// You can insertBefore() too.

// Or... document.body.innerHTML = document.body.innerHTML + '<div horizontal layout></div>';

}
</script>
</head>
<body>
</body>
</html>

一些有用的链接:

http://www.w3schools.com/jsref/met_document_createelement.asp

Adding elements to the DOM

Searching elements in DOM

How to add or update an attribute to an HTML element using JavaScript?

等等。

相关问题