为什么JavaScript不能运行?
Why won't JavaScript run?
我正在制作一个HTML代码编辑器,我测试了我知道的所有HTML标签,除了脚本标签外,它们都有效。
当我在文本区输入<script>something</script>
并单击按钮时,脚本不会执行。
请帮忙!下面是代码:
<span id="finishedProduct">
<p>When you enter code, your finished product will be here! Don't worry, if you make a mistake you can fix it later!</p>
</span>
<form name="userCode">
<textarea name="userCode" cols="90" rows="20" placeholder="Type your code here"></textarea></br>
<a href="javascript:makeCode()"><button type="button">Run Code!</button></a>
</form>
<script>
function makeCode() {
var userCode=document.forms["userCode"]["userCode"].value;
document.getElementById('finishedProduct').innerHTML = userCode;
}
</script>
工作代码:
<span id="finishedProduct">When you enter code, your finished product will be here! Don't worry, if you make a mistake you can fix it later!
</span>
<form name="userCode">
<textarea name="userCode" cols="90" rows="20" placeholder="Type your code here"></textarea>
<br/>
<button type="button" onClick="makeCode()">Run Code!</button>
</form>
<script type="text/javascript">
function makeCode() {
var userCode=document.forms["userCode"]["userCode"].value;
document.getElementById('finishedProduct').innerHTML = userCode;
}
</script>
这里是JSFiddle的链接:http://jsfiddle.net/Q2qLF/。我删除了一些破损的HTML,例如;一个按钮不应该包含在锚标记中,我在你的按钮中添加了一个'onclick',它将调用'makeCode()'函数,我在你的脚本标签中添加了'type="text/javascript"',因为这可以最大限度地提高兼容性。
如果您还需要什么帮助,请告诉我
我已经更新了JSFiddle http://jsfiddle.net/Xanco/Q2qLF/1/现在有两个文本区域,一个用于HTML,一个用于Javascript。我还创建了一个名为'makejs'的新函数,它采用Javascript文本区的值并通过'eval'运行它-这执行传递给它的Javascript。
我把答案放在这里:http://jsfiddle.net/joshnicholson/P8eh9/
我不知道你为什么要把button元素包装在锚中,但我会用一种稍微不同的方式来做。
下面是修改后的javascript:
var myButton = document.getElementById("btnRunCode");
myButton.addEventListener("click", makeCode);
function makeCode() {
var userCode=document.forms["userCode"]["userCode"].value;
document.getElementById('finishedProduct').innerHTML = userCode;
}
我添加了一个id"btnRunCode"到你的按钮元素,只是为了让事情对我来说更容易。
相关文章:
- Javascript赢得'不能在IE上编译,但可以在Chrome上运行
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- 漂亮的照片在Dreamweaver上完美运行;但不能在任何浏览器上在线工作
- 我的简单Javascript代码可以在Safari上运行,但不能在Chrome、Firefox或Opera上运行
- d3实现可以在firefox/chrome上运行,但不能在iPad上运行
- JS函数如何/为什么不能'不能在代码中执行,但在chrome控制台中运行良好
- 从Java脚本返回多个值,这些值不能在firefox上运行(在IE上运行良好)
- 如何将嵌套承诺与Q连锁?我可以'Don’我不能让它们按正确的顺序运行
- Javascript可以在Safari上运行,但不能在任何其他浏览器上运行
- Fullpage.js不能在Mozilla上运行,但在Chrome上运行良好
- 为什么我的 php 文件不能从我的 JavaScript 运行
- 为什么这个简单的函数不能在这个javascript状态机中的这个对象上运行
- jQuery不能在PHP应用程序中运行
- 不能让CKEditor插入HTML在IE 8上运行
- 为什么我的HTML5 / JS应用程序不能在我的iPhone上运行
- ASP.NET 应用在VS2010中运行,但在发布/部署后不能运行
- 为什么 $.post 似乎不能在我的网站上的 iPad/iPhone 上运行
- 为什么 MutationObserver 代码不能在 Chrome 30 上运行
- 为什么我不能在谷歌浏览器中运行我的任何JavaScript
- Javascript 不能在 PartialView 上运行