Javascript程序可以在Eclipse中运行,但不能在notepad++中运行

Javascript program runs in Eclipse but not in Notepad++

本文关键字:运行 但不能 notepad++ Eclipse 程序 Javascript      更新时间:2023-09-26

当我使用JavaScript点击按钮时,我试图动态地在网页中插入文本框。我是JavaScript的初学者。我尝试在Chrome, Firefox和Opera(启用JavaScript)中执行该程序。但是,当我单击该按钮时,程序不插入文本框。我尝试在Eclipse中运行相同的代码,并且在Eclipse中的HTTP预览器运行相同的代码。有没有人能告诉我为什么会发生这种情况,以及我如何在浏览器中执行相同的代码?

下面是我使用的代码:
<html>
<head>
<title>Doing some random programming</title>
</head>
<body>
Add input elements here:
<br/>
<input type = "button" value = "Add another element" id = "AddButton" onClick = "AddTextBox()" />
<div id = "Division"></div>
<script type = "text/javascript">   
<!--
//var addButton = document.getElementById('AddButton');
function AddTextBox()
 {      
    var divis = document.getElementById('Division');
    var inputTxt = document.createElement("<input type = '"text'" />");
    var div = document.createElement('div');
    //input.type = "text";      
    div.appendChild(inputTxt);
    divis.appendChild(div);
}
//-->
</script>
<noscript>
    Needs javascript
</noscript>
</body>
</html>

createElement方法接受元素类型的名称,而不是HTML字符串。

var inputTxt = document.createElement("<input type = '"text'" />");
应该

var inputTxt = document.createElement("input");

或者(如果您想显式地声明type="text",您不需要这样做,因为它是默认的):

var inputTxt = document.createElement("input");
inputTxt.setAttribute('type', 'text');
相关文章: