如何从js文件获取功能到htm文件

时间:2017-07-17 12:12:14

标签: jquery html

我有一个包含此代码的htm文件:

<div id="this-item">
    <a onclick="myFunction()">
        <i class="fa fa-overview"> </i>
        <span class="nav-item"> Overview </span>
    </a>
</div> 

和js文件:

function myFunction()
{
    $("#this-item").show();
}

window.initDashboard = function () {

};

我在这里尝试的是进入htm myFunction()的功能。单击文本时,它应该显示一些内容,但现在我收到错误说:

  

未捕获的ReferenceError:myFunction未定义

我做错了什么,我该如何解决?

3 个答案:

答案 0 :(得分:1)

删除代码中的点

curl 'http://192.168.208.9:8383/login' -H 'Host: 192.168.208.9:8383' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/x-www-form-urlencoded' -H 'Referer: http://192.168.208.9:8383/login' -H 'Cookie: JSESSIONID=ECEB101AEB52EDF7409F6E249249D621' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' --data 'username=alice&password=blockchain'

答案 1 :(得分:0)

试试这个

function myFunction() {
alert('some text');
  $("#this-item").html('some text');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="this-item">
  <a onclick="myFunction()">
    <i class="fa fa-overview"> </i>
    <span class="nav-item"> Overview </span>
  </a>
</div>

show()没有用,已经显示出来了。所以,用'some text'替换html内容只是为了展示myfunction()正在运行。

答案 2 :(得分:0)

您是否在HTML中使用myFunction()导入了js文件?有关更多信息,请参阅此文档

https://www.w3schools.com/tags/att_script_src.asp