function helloworld() {
alert('hello world');
}静态html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="helloworld.js" type="text/javascript"></script>
</head>
<body>
<form action="#">
<input type="button" onclick="helloworld()" />
</form>
</body>
</html>使用http://accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/进行第一次转换:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="helloworld.js" type="text/javascript"></script>
</head>
<body>
<form action="#">
<script type="text/javascript">
document.write("<input type=\"button\" onclick=\"helloworld()\" \/>");
</script>
</form>
</body>
</html>第二次转换不起作用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="helloworld.js" type="text/javascript"></script>
</head>
<body>
<form action="#">
<script type="text/javascript">
document.write(" <script type=\"text\/javascript\">");
document.write(" document.write(\"<input type=\\"button\\" onclick=\\"helloworld()\\" \\/>\");");
document.write(" <\/script>");
</script>
</form>
</body>
</html>有没有可能纠正第二个代码,使它可以工作?
发布于 2010-10-13 21:19:05
<script type="text/javascript">
document.write(" <script type=\"text\/javascript\">");
document.write("document.write('<input type=\"button\" onclick=\"helloworld()\" />');");
document.write(" <\/script>");
</script>将输出
编辑
检查完fire bug后,生成的代码如下所示。
<script type="text/javascript">
document.write('<input type="button" onclick="helloworld()" />');
</script>
<input onclick="helloworld()" type="button">发布于 2010-10-13 21:19:49
这样如何:
..
<form action='#'>
</form>
<script type="text/javascript">
window.addEventListener('load', function() {
var inp = document.createElement('input');
inp.setAttribute('type', 'button');
input.addEventListener('click', function() {
..
}, false);
document.forms[0].appendChild(inp);
}, false);
</script>当然,它更长,但它是有效的,这是做这件事的“适当”方式(使用jQuery之外)。
</body>
</html>https://stackoverflow.com/questions/3923962
复制相似问题