作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 页。 one.html 和 two.html
第一个 html 页面包含一个带有 java 脚本函数 modify(d) 的脚本标记。在 two.html 中我想调用这个相同的函数而不是再次重新键入它。我该怎么做?我需要在页面之间创建链接吗?如果是,怎么做?
我试过在第二个文件中使用。但我不断收到错误消息:控制台中出现“修改(d)未定义”。
这两个文件在我的文件夹结构中并排放置,因此路径应该是正确的。
提前致谢。
最佳答案
如果你删除内联脚本(它也适用于内联样式)并这样做,你将函数(和规则)存储在它自己的文件中,你将能够重用它(它们),如下所示下面的示例
文件:one.html
<!DOCTYPE html>
<html>
<head>
<title>Page One</title>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<link rel='stylesheet' type='text/css' href='css/style.css' />
<script src="js/script.js"></script>
</head>
<body>
Page one content
</body>
</html>
文件:two.html
<!DOCTYPE html>
<html>
<head>
<title>Page two</title>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<link rel='stylesheet' type='text/css' href='css/style.css' />
<script src="js/script.js"></script>
</head>
<body>
Page two content
</body>
</html>
文件:样式.css
body {
background: url(../images/bkg.png);
}
文件:script.js
function modify(d) {
....
}
并且让你的文件像这样放在你的文件夹中
/www/one.html
/www/two.html
/www/js/script.js
/www/css/style.css
/www/images/bkg.png
关于javascript - 如何在一个html页面中调用另一个html页面中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40946465/
我是一名优秀的程序员,十分优秀!