Bootstrap按钮onClick不起作用

时间:2017-04-26 16:16:23

标签: javascript jquery html twitter-bootstrap

我使用bootstrap并有一个简单的布局,包括一个按钮。我尝试了here的代码解决方案,但它无效。这是我的代码,它甚至没有显示' test'点击搜索按钮后。

    <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Die drei oberen meta tags *müssen* zuerst im Head-Tag angegeben werden; alle anderen Tags können anschließend eingefügt werden -->
  <title>Search</title>

  <!-- Bootstrap -->
  <link href="css/bootstrap.min.css" rel="stylesheet">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
      <![endif]-->
    </head>
    <body>
    <div class="container-fluid">
      <h1>Welcome!</h1>
    </br>
    <button id="search" type="button" class="btn btn-primary">Suchen</button>
    <p><br/></p>
    <p id="output"></p>
    </div>
    <script type="text/javascript">
      $('#search').on('click', function (e) {
        document.getElementById('#output').innerHTML = "test";

        var EventSearch = require("facebook-events-by-location-core");

        var es = new EventSearch({
          "lat": 40.710803,
          "lng": -73.964040
        });

        es.search().then(function (events) {
          console.log(JSON.stringify(events));
          document.getElementById('output').innerHTML = JSON.stringify(events);
        }).catch(function (error) {
          console.error(JSON.stringify(error));
        }); 
      })
    </script>

    <!-- jQuery (notwendig für Bootstraps JavaScript plugins) -->
    <script src="js/jquery-3.2.1.min"></script>
    <!-- Alle kompilierten plugins einbeziehen -->
    <script src="js/bootstrap.min.js"></script>
  </body>
  </html>

2 个答案:

答案 0 :(得分:0)

使用document.getElementByid()时,您不需要id之前的#

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Die drei oberen meta tags *müssen* zuerst im Head-Tag angegeben werden; alle anderen Tags können anschließend eingefügt werden -->
  <title>Search</title>

  <!-- Bootstrap -->
  <link href="css/bootstrap.min.css" rel="stylesheet">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
      <![endif]-->
    </head>
    <body>
    <div class="container-fluid">
      <h1>Welcome!</h1>
    </br>
    <button id="search" type="button" class="btn btn-primary">Suchen</button>
    <p><br/></p>
    <p id="output"></p>
    </div>
    <script type="text/javascript">
      $('#search').on('click', function (e) {
        document.getElementById('output').innerHTML = "test";
        // I removed the event search code because I do not have the files and it was breaking the example.
      })
    </script>

    <!-- jQuery (notwendig für Bootstraps JavaScript plugins) -->
    <script src="js/jquery-3.2.1.min"></script>
    <!-- Alle kompilierten plugins einbeziehen -->
    <script src="js/bootstrap.min.js"></script>
  </body>
  </html>

答案 1 :(得分:0)

首先,不要使用&#34;#&#34;使用document.getElementById时签名。其次,你需要添加&#34;要求&#34;参考

检查控制台消息:

&#13;
&#13;
 $('#search').on('click', function (e) {
        document.getElementById('output').innerHTML = "test";

        var EventSearch = require("facebook-events-by-location-core");

        var es = new EventSearch({
          "lat": 40.710803,
          "lng": -73.964040
        });

        es.search().then(function (events) {
          console.log(JSON.stringify(events));
          document.getElementById('output').innerHTML = JSON.stringify(events);
        }).catch(function (error) {
          console.error(JSON.stringify(error));
        }); 
      })
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container-fluid">
      <h1>Welcome!</h1>
    </br>
    <button id="search" type="button" class="btn btn-primary">Suchen</button>
    <p><br/></p>
    <p id="output"></p>
    </div>
&#13;
&#13;
&#13;