我是 CoffeeScript 的新手。在包含我的 CoffeeScript 文件functions.coffee后,生成以下文件main.js:
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
require('../../modules/common/frontend/functions');
},{"../../modules/common/frontend/functions":2}],2:[function(require,module,exports){
//Link selection in menu
function setCurrentlink(){
var currenturl=window.location.href;
var part=currenturl.match(/\n\/displaywizard\/(.*)$/g);
alert(part);
}
...
},{}]},{},[1])
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyaWZ5L25vZGVfbW9kdWxlcy9icm93c2VyLXBhY2sv...
我一直在尝试从 html 文件调用函数 setCurrentlink ,如下所示:
<script type="text/javascript">setCurrentlink();</script>
我尝试将 js 文件的链接放在页面底部的 head 部分,但收到错误消息“找不到变量 setCurrentlink”。请帮助我找出我的错误或我遗漏的任何内容。
functions.coffee
#@export functions
#Link selection in menu
setCurrentlink = ->
currenturl = window.location.href
part = currenturl.match(/\n\/displaywizard\/(.*)$/g)
alert currenturl
return
...
最佳答案
您的函数是在函数内定义的,因此它的范围仅限于该函数。您需要全局定义您的函数才能使其工作。也就是说,您可能会以错误的方式处理这个问题。如果我有更多关于您想要完成的任务的信息,我可以提供进一步的建议。
关于javascript - 从 html 文件中的 CoffeeScript 文件调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731122/