我在html文件中的函数不适用于我写得更高的错误。当我从中更改它时
功能名称(...)到window.name = function(...)
但它无论如何都不起作用并写入
“write.name不是finction”
我的代码
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
function changeText(id) {
id.innerHTML = "Ooops!";
}
function displayDate () {
document.getElementById("demo").innerHTML = Date();
}
function addField () {
document.getElementById("demo").innerHTML = Date();
}
function addInput(divName){
var newdiv = document.createElement('div');
newdiv.innerHTML = "<br>Select File to Upload:<br><input type="file" onchange="addInput('dynamicInput');" name="fileName">";
document.getElementById(divName).appendChild(newdiv);
window.displayDate();
window.addField();
}
</script>
</head>
<body>
<h1 id="fff" onclick="changeText(this);">Click on this text!</h1>
<form action="/getimage" method="post" enctype="multipart/form-data">
<div id="dynamicInput">
<br>Select File to Upload:<br><input type="file" onchange="addInput('dynamicInput');" name="fileName">
</div>
<input type="submit" value="Upload">
<input type="button" value="Add another input" onClick="addInput('dynamicInput');">
</form>
<p id="demo"></p>
</body>
请帮忙!
答案 0 :(得分:0)
您需要解决quotes
问题\'
和字符串onchange="addInput(\'dynamicInput\');"
。而window.name
未在您的dom中定义
function changeText(id) {
id.innerHTML = "Ooops!";
}
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
function addField() {
document.getElementById("demo").innerHTML = Date();
}
function addInput(divName) {
var newdiv = document.createElement('div');
newdiv.innerHTML = '<br>Select File to Upload:<br><input type="file" onchange="addInput(\'dynamicInput\');" name="fileName">';
document.getElementById(divName).appendChild(newdiv);
window.displayDate();
window.addField();
}
&#13;
<h1 id="fff" onclick="changeText(this);">Click on this text!</h1>
<form action="/getimage" method="post" enctype="multipart/form-data">
<div id="dynamicInput">
<br>Select File to Upload:<br><input type="file" onchange="addInput('dynamicInput');" name="fileName">
</div>
<input type="submit" value="Upload">
<input type="button" value="Add another input" onClick="addInput('dynamicInput');">
</form>
<p id="demo"></p>
&#13;
答案 1 :(得分:0)
请注意,您需要转义双引号才能在另一个双引号内使用它们,或者使用单引号代替。
newdiv.innerHTML = "<br>Select File to Upload:<br><input type='file' onchange='addInput('dynamicInput');' name='fileName'>";