如何调用外部jQuery

时间:2015-08-20 08:23:32

标签: jquery html

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="/Users/skhare/myFirstStyleSheet.css">

        <script src="http://code.jquery.com/jquery-1.7.1.min.js"> </script>

        <script>
                function myFunction(a)
                {
                    window.alert("hi " + a);
                    $.getJSON('/Users/skhare/tableList.json', function(tableList) {
                        var output="<table id=tableStyle>";
                        output+="<tr>" + "<th>" + "Table Names" + "</th>" + "</tr>";
                        for (var i in tableList.t)
                        {
                            output+="<tr>" + "<td>" + "<a href=/Users/skhare/tableDescription.html>" + tableList.t[i].name + "</a>" + "</td>" + "</tr>";
                        }

                        output+="</table>";
                        document.getElementById("placeholder1").innerHTML=output;
                    });
                }
        </script>
        <script>
          $(document).ready(function(){
            $.getJSON('/Users/skhare/reportSuiteList.json', function(reportSuiteList) {
              var output="<table id=tableStyle>";
              output+="<tr>" + "<th>" + "id" + "</th>" + "<th>" + "name" + "</th>" + "<th>" + "stage" + "</th>" + "<th>" + "DWH" + "</th>" + "</tr>";
              for (var i in reportSuiteList.suites)
              {
                output+="<tr>" + "<td>" + reportSuiteList.suites[i].REPORTSUITE_ID + "</td>" + "<td>" + 
                "<button class=\"report-suites\" id =\""+ reportSuiteList.suites[i].REPORTSUITE_NAME+ "\" onclick= \"myFunction(\'" + reportSuiteList.suites[i].REPORTSUITE_NAME + "\')\">"+reportSuiteList.suites[i].REPORTSUITE_NAME + "</button>" +
                "</td>" + "<td>" + reportSuiteList.suites[i].STAGING_DATABASE + "</td>" + "<td>" + reportSuiteList.suites[i].DWH_DATABASE + "</td>" + "</tr>";
              }

              output+="</table>";
              document.getElementById("placeholder").innerHTML=output;
            });
          });
          </script>
      </head>

      <body>
          <div id="placeholder"></div>
          <br><br><br>
          <div id="placeholder1"></div>

      </body>
      </html>

我想点击按钮调用myfunction(a)。我尝试过,但myfunction()只显示警告信息。一种选择是放置 myFunction中的脚本(a)在其他html文件中调用文件onclick of button但我不知道该怎么做。 请帮忙

1 个答案:

答案 0 :(得分:2)

要创建自己的脚本,请打开记事本并输入所有JavaScript内容(请勿在此处使用myscript.js)并将其另存为<script src="http://code.jquery.com/jquery-1.7.1.min.js"> </script> <script src="myscript.js"> </script>

在您的HTML文件中,将其包含为:

myscript.js

确保在加载jQuery后包含它。

在你的$(document).ready(function () { // All your code here... }); 中,要完美,请将所有代码放入其中:

myscript.js

根据您的问题,function myFunction(a) { window.alert("hi " + a); $.getJSON('/Users/skhare/tableList.json', function(tableList) { var output="<table id=tableStyle>"; output+="<tr>" + "<th>" + "Table Names" + "</th>" + "</tr>"; for (var i in tableList.t) { output+="<tr>" + "<td>" + "<a href=/Users/skhare/tableDescription.html>" + tableList.t[i].name + "</a>" + "</td>" + "</tr>"; } output+="</table>"; document.getElementById("placeholder1").innerHTML=output; }); } $(document).ready(function(){ $.getJSON('/Users/skhare/reportSuiteList.json', function(reportSuiteList) { var output="<table id=tableStyle>"; output+="<tr>" + "<th>" + "id" + "</th>" + "<th>" + "name" + "</th>" + "<th>" + "stage" + "</th>" + "<th>" + "DWH" + "</th>" + "</tr>"; for (var i in reportSuiteList.suites) { output+="<tr>" + "<td>" + reportSuiteList.suites[i].REPORTSUITE_ID + "</td>" + "<td>" + "<button class=\"report-suites\" id =\""+ reportSuiteList.suites[i].REPORTSUITE_NAME+ "\" onclick= \"myFunction(\'" + reportSuiteList.suites[i].REPORTSUITE_NAME + "\')\">"+reportSuiteList.suites[i].REPORTSUITE_NAME + "</button>" + "</td>" + "<td>" + reportSuiteList.suites[i].STAGING_DATABASE + "</td>" + "<td>" + reportSuiteList.suites[i].DWH_DATABASE + "</td>" + "</tr>"; } output+="</table>"; document.getElementById("placeholder").innerHTML=output; }); }); 内容应仅 以下内容:

   printf("%p", (void*)c);