[英]Creating Html Element with onclick() event in javascript
I've been working at this for the last hour, but I am still unable to get the expected output... All I want is to create an HTML element using the onclick
event. 我一直在这个工作的最后一个小时,但我仍然无法获得预期的输出...我想要的是使用
onclick
事件创建一个HTML元素。 I was able to create the element on load, but find myself unable to with the event. 我能够在加载时创建元素,但发现自己无法使用该事件。 Please lead me in the right direction.
请带领我朝着正确的方向前进。 Here's my HTML page:
这是我的HTML页面:
<html>
<body>
<div id="d1">
<p id="p1">Paragraph.</p>
</div>
<div id="d2">
<label onclick="open()">Inbox</label>
</div>
<script>
function open(){
alert("Start");
var para=document.createElement("p");
var node=document.createTextNode("Text");
para.appendChild(node);
para.style.color="red";
var element=document.getElementById("d2");
element.appendChild(para);
}
</script>
</body>
</html>
open()
seems to be a reserved functions. open()
似乎是一个保留函数。 Tried with open1()
, it works. 尝试使用
open1()
,它的工作原理。
您可能正在与浏览器中内置的window.open函数发生冲突,重命名您的函数。
Try this code 试试这个代码
<label onclick="create_elem();">Inbox</label>
<script language="javascript">
function create_elem(){
alert("Start");
var para=document.createElement("p");
var node=document.createTextNode("Text");
para.appendChild(node);
para.style.color="red";
var element=document.getElementById("d2");
element.appendChild(para);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.