问题描述
HTML文件是使用Markdown生成的.我在HTML中包含了一个JavaScript文件.可见它已加载到浏览器中(在Mozilla firefox上尝试过).
The HTML file is generated using Markdown.I have included a JavaScript file in HTML. It is visible that it is loaded in the browser (tried on Mozilla firefox).
请参见下面的证明.
See the proof below.
我从 https://www.whatismybrowser中测试了是否启用了JS. com/detect/is-javascript-enabled
但是不知何故,它没有被执行.
But somehow, it is not being executed.
下面是我生成的HTML
Below is my generated HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-16"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="generator" content="Bootply-JBake-Custom"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-16"/>
<title>Title</title>
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/asciidoctor.css" rel="stylesheet">
<link href="/css/base.css" rel="stylesheet">
<link href="/css/prettify.css" rel="stylesheet">
<link href="/css/styles.css" rel="stylesheet">
<link href="/css/animate.css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico">
<script src="/js/jquery-1.11.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<div id="wrap">
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a href="/index.html" class="navbar-brand">LOGO</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbar-main">
<ul class="nav navbar-nav">
<li><a href="/projects/projects.html">Projects</a></li>
<li><a href="/about.html">About</a></li>
<li><a href="/categories.html">Categories</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="page-header" id="banner">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-6">
<h1>Projects</h1>
</div>
</div>
</div>
<!-- This script is loading but not executing -->
<script type="text /javascript" src="projects.js"></script>
<p>Here is the list of projects</p>
</div>
</div>
<div id="push"></div>
<div id="footer">
<div class="container">
</div>
</div>
</body>
</html>
projects.js
projects.js
$(document).ready(function(){
console.log("ready")
alert("hi");
});
console.log("loaded")
推荐答案
<script type="text /javascript" src="projects.js"></script>
删除结果type="text /javascript
中的空格:
<script type="text/javascript" src="projects.js"></script>
这应该允许浏览器正确解析HTML并加载脚本.
This should allow the browser to parse the HTML correctly and load the script.
或完全删除<script>
标记中的type
属性,如下面的注释所建议.
Or remove the type
attribute from the <script>
tag entirely, as suggested by the comment below.
这篇关于JS文件以HTML加载(通过Markdown生成),但未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!