我在github here上找到了Select2的启动插件,似乎无法在我的网站上正常工作。我将在下面粘贴我的代码。我无法确定服务器上是否已经有太多竞争的CSS,或者我只是将东西放置在错误的位置。我对使用这些东西有点陌生,但我正在努力弄清所有这些东西。

  <?php
/*==================================================================================
README:

This global var (`root_dir`) must be set to the relative path
of the root directory.

E.g.
 If this page is in "career.clemson.edu/coop/"
 then the line should read
    $GLOBALS["root_dir"]="../";
 or if this page is in "career.clemson.edu/coop/students/";
 then the line should read
    $GLOBALS["root_dir"]="../../";


The template automatically includes common meta elemtents (keywords, etc),
common CSS files (ours, bootstrap, font-awesome), a fallback fon non-HTML5 compliant
browsers, and some basic javasript files at the end of the page.
Feel free to add any other things as needed on a per page basis!

All includes *should* in %root_dir%/includes


==================================================================================*/
    $GLOBALS["root_dir"]="../";
    require_once $GLOBALS["root_dir"].'includes.inc';
?>
<!doctype html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
    <?php
        require_once $GLOBALS['include_loc']."common_meta.inc";
        require_once $GLOBALS['include_loc']."common_css.inc";
        require_once $GLOBALS['include_loc']."html5_shiv.inc";
    ?>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">

  <title>Resumes and Cover Letters</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  <!-- jQuery library -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- Latest compiled JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>


</head>

<body>
    <?php require_once $GLOBALS["include_loc"]."top_menu.inc";?> <!-- Top Menu -->
    <div class span="row">
    <script type="text/javascript">
        $(".js-example-placeholder-multiple").select2({
        placeholder: "Select a state"
        });
    </script>
    <select class="js-example-placeholder-multiple">
        <option value="AL">Alabama</option>
        ...
        <option value="WY">Wyoming</option>
    </select>



    <?php require_once $GLOBALS["include_loc"]."footer.inc";?>  <!-- Footer -->
    <?php require_once $GLOBALS['include_loc']."basic_js.inc"; ?>   <!--Include JS Files at the end for speed-->
</body>
</html>

最佳答案

这里有四件事是错的。


您尚未包括Bootstrap JS。


为此,只需包括以下内容:

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<head>标记或<body>标记中。


您也没有包含jQuery。 Bootstrap和select2都需要jQuery。


要包含jQuery,请插入以下代码:

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<head>标记或<body>标记中,但请确保它在Bootstrap和select2 JS之前。


您的<select>具有类js-example-basic-single,但是您正在类js-example-placeholder-multiple上调用select2插件。


解决此问题,插件应能正常工作。


(这是次要的)此div:<div class span="row">在代码中未用</div>关闭。另外,<div class span="row">似乎不太正确。




$(".js-example-basic-single").select2({
  placeholder: "Select a state"
});

<html>
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">

  <title>Resumes and Cover Letters</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
  <!-- Top Menu -->
  <div class span="row">
    <select class="js-example-basic-single">
        <option value="AL">Alabama</option>
        <option value="WY">Wyoming</option>
    </select>
  </div>
</body>
</html>

关于javascript - Bootstrap CSS无法正确加载,看起来像是典型的HTML/CSS下拉菜单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45739497/

10-11 05:31
查看更多