我的 innerHTML 命令没有改变任何东西,我不知道为什么
My innerHTML command isn't changing anything and i can't figure out why
代码如下:
<div id="js"><button onclick="document.getElementById('js').innerHTML=('<form>
<input type=text name=tick1></input>
<input type=text name=tick2></input>
<input type=text name=tick3></input>
<input type=submit></input>
</form>')">Kaarten bestellen</button>
</div>
我真的不明白为什么它不起作用。当我将表单作为实际的html放在脚本之外时,它可以正常工作,当我使用更像.innerHTML('test')的东西时,它工作得很好。我在这里不知所措。
您需要
在多行字符串中的每一行末尾添加'
(请参阅 JavaScript 中的多行字符串变量):
<div id="js"><button onclick="document.getElementById('js').innerHTML=('<form>'
<input type=text name=tick1></input>'
<input type=text name=tick2></input>'
<input type=text name=tick3></input>'
<input type=submit></input>'
</form>')">Kaarten bestellen</button>
</div>
http://jsfiddle.net/o5tuqt1L/
它工作正常。您不能在此 js 代码中使用换行符。您可以使用 + 运算符来连接字符串。
document.getElementById('js').innerHTML=('<form>'
+'<input type=text name=tick1></input><input type=text name=tick2>'
+'</input><input type=text name=tick3></input><input type=submit>'
+'</input></form>');
工作中的 JSFIDDLE
字符串文字不是封闭的,因为它有多行...你为什么不把它藏起来呢?
<div id="js">
<button onclick="document.getElementById('myForm').style.display='block';this.style.display='none';">
Kaarten bestellen
</button>
<form id="myForm" style="display:none">
<input type=text name=tick1></input>
<input type=text name=tick2></input>
<input type=text name=tick3></input>
<input type=submit></input>
</form>
</div>
Javascript的innerHTML
是一个属性,而不是一个方法,所以你只是像这样给它赋值
document.getElementById('js').innerHTML = "Hello, World.";
内部网页解释
您的 HTML 语法无效:
<form>
<input type=text name=tick1></input>
<input type=text name=tick2></input>
<input type=text name=tick3></input>
<input type=submit></input>
</form>
您的type
和name
属性值需要用双引号括起来"
.input
元素也不需要内容,因此您可以使其自动关闭。检查这个:
<form>
<input type="text" name="tick1" />
<input type="text" name="tick2" />
<input type="text" name="tick3" />
<input type="submit" />
</form>
Javascript字符串不是多行的。所以你需要转义换行符,如下所示:
<form>'
<input type="text" name="tick1" />'
<input type="text" name="tick2" />'
<input type="text" name="tick3" />'
<input type="submit" />'
</form>
多行 JavaScript 字符串的解释
如果我以正确的方式理解您的问题,这也是一个答案。
<div id="js"><button onclick="document.getElementById('js').innerHTML='<form>'
+'<input type=''text'' name=''tick1''></input>'
+'<input type=''text'' name=''tick2''></input>'
+'<input type=''text'' name=''tick3''></input>'
+'<input type=''submit''></input>'
+'</form>'">Kaarten bestellen</button>
</div>
试试这个
<div id="js"><button onclick="document.getElementById('js').innerHTML='<form><input type=text name=tick1></input><input type=text name=tick2></input><input type=text name=tick3></input><input type=submit></input></form>'">Kaarten bestellen</button>
</div>
<div id="js"><button onclick="document.getElementById('js').innerHTML=''
<form>'
<input type=text name=tick1></input>'
<input type=text name=tick2></input>'
<input type=text name=tick3></input>'
<input type=submit></input>'
</form>'
'">Kaarten bestellen</button>
</div>
我认为问题的原因是因为要显示的内容跨越了几行而没有正确连接。我重新设计如下;
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<script type="text/javascript">
function loadForm(){
var formContent ="<form>"
+"<input type='text' name='tick1'></input>"
+"<input type='text' name='tick2'></input>"
+"<input type='text' name='tick3'></input>"
+"<input type='submit'></input>"
+"</form>";
document.getElementById('js').innerHTML =(formContent);
}
</script>
<div id="js">
<button onclick="loadForm()">Kaarten bestellen</button>
</div>
</body>
</html>
我认为这也使代码更干净。
相关文章:
- 这个旋钮没有更新;我不知道为什么
- 我不知道为什么我的代码是错误的?又有什么错
- I'我不知道为什么我的代码没有'不起作用
- sqlite查询返回错误-can'我不知道为什么
- 我没有'我不知道为什么我的jqGrid子网格没有'不要给我看数据
- 我的素数函数坏了,但我没有;我不知道为什么
- 当我从struts2操作在js函数中获得JSONArray时,它是空的,我不知道为什么
- 可以'我不知道为什么这个数字赢了;t增量
- 我不知道为什么ng repeat在我的代码中不起作用
- 历史.推送状态错误,我不知道为什么
- 我的 innerHTML 命令没有改变任何东西,我不知道为什么
- JavaScript Validator 一直在说关于第 11 行的意外“如果”,我不知道为什么
- 我的地理位置(javascript)不起作用,我不知道为什么
- 我不知道为什么这个画布是空的
- 石头剪刀布蜥蜴斯波克游戏逻辑偶尔会输出错误的赢家,我不知道为什么
- ajax 请求失败,我不知道为什么
- 我的网站运行缓慢,我不知道为什么
- 我不知道为什么我在 JavaScript 中的函数 Flip() 没有返回任何内容.我希望它是正面或反面,它就会返回
- 帆布罪恶曲线模糊了很多.我不知道为什么它模糊了
- JQuery 无法加载.我不知道为什么.这是一个完全的谜