【问题标题】:Js var, not defined [duplicate]Js var,未定义[重复]
【发布时间】:2017-05-15 23:42:25
【问题描述】:

大家好,由于某种原因,这个变量对我的文件不起作用。我有一张图片,当用户将鼠标悬停在它上面时,它假设会弹出一个滑动框,其中包含有关图片的一些信息。但是,由于某种原因,该框不会出现。

HTML/JS:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
    <link href="css/custom.css" rel="stylesheet">
    <script type="text/javascript">
    var modal = $('.modal');

    modal.hover(function() {
        $(this).toggleClass('open');
    });

    alsolike(
      "qEybdR", "Checkbox Radial Wash",
      "dPdoNp", "Google Messenger Icon Button",
      "BJAjF", "Masonry Multi Column Grid"
    );
    </script>
</head>
<body>

<a class="drib" href="http://drbl.in/okRS">View it on Dribbble <i class="fa fa-dribbble"></i></a>
<div class="modal">
  <div class="image-container"></div>
  <div class="content">
    <div class="wrapper">
      <div class="category">First Drive Review</div>
      <h2>2015 Dodge Challenger SRT Hellcat</h2>
      <h4>Andy Wendler <span>from</span> July 2014</h4>
      <div class="line"></div>
      <p>Make room, Beelzebub, there’s a new demon-prince pony car in town, and it’s from the people who once brought you a real Demon. Known in this mortal realm as the Dodge Challenger SRT Hellcat...</p><a href="http://www.caranddriver.com/reviews/2015-dodge-challenger-srt-hellcat-first-drive-review">Read More <i class="fa fa-caret-right"></i></a>
    </div>
  </div>
</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>
</html>

CSS:

.modal {
  width: 600px;
  height: 400px;
  margin: 50px auto;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
  position: relative;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}
.modal .image-container {
  background: url("http://media.caranddriver.com/images/14q3/612022/2015-dodge-challenger-srt-hellcat-first-drive-review-car-and-driver-photo-615298-s-original.jpg");
  background-size: cover;
  background-position: center center;
  display: inline-block;
  width: 100%;
  height: 100%;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}
.modal .content {
  width: 40%;
  height: 100%;
  position: absolute;
  right: -40%;
  top: 0;
  background: white;
  box-shadow: -10px 0 25px rgba(0, 0, 0, 0.5);
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}
.modal .wrapper {
  width: 100%;
  height: 100%;
  position: relative;
  padding: 24px;
}
.modal .wrapper h2 {
  font-family: "Georgia", serif;
  font-style: italic;
  font-weight: bold;
  font-size: 26px;
  line-height: 32px;
  margin: 8px 0 10px 20px;
  opacity: 0;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-transition-delay: 0.2s;
          transition-delay: 0.2s;
}
.modal .wrapper h4 {
  text-transform: uppercase;
  color: #999;
  font-size: 12px;
  position: relative;
  top: 4px;
  opacity: 0;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-transition-delay: 0.3s;
          transition-delay: 0.3s;
}
.modal .wrapper h4 span {
  text-transform: none;
  font-style: italic;
  font-family: "Georgia", serif;
}
.modal .wrapper .category {
  background: #333;
  padding: 7px 15px;
  display: inline-block;
  color: white;
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 1px;
  font-weight: 500;
  position: absolute;
  top: -24px;
  left: 20px;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-transition-delay: 0.5s;
          transition-delay: 0.5s;
}
.modal .wrapper .line {
  width: 50px;
  height: 2px;
  opacity: 0;
  background: #E3000C;
  margin: 16px 0 14px;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-transition-delay: 0.4s;
          transition-delay: 0.4s;
}
.modal .wrapper p {
  font-size: 14px;
  line-height: 24px;
  opacity: 0;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  -webkit-transition-delay: 0.5s;
          transition-delay: 0.5s;
}
.modal .wrapper p span {
  text-transform: uppercase;
  font-size: 0.75em;
  font-weight: 500;
  letter-spacing: 0.5px;
}
.modal .wrapper a {
  display: block;
  text-align: right;
  text-decoration: none;
  font-style: italic;
  font-size: 12px;
  color: #999;
  font-family: "Georgia", serif;
  margin-top: 12px;
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  opacity: 0;
}
.modal .wrapper a i.fa {
  -webkit-transition: all 0.2s;
  transition: all 0.2s;
  margin-left: 2px;
  color: #E3000C;
}
.modal .wrapper a:hover {
  color: #E3000C;
}
.modal .wrapper a:hover i.fa {
  margin-left: 12px;
}

.modal.open .image-container {
  width: 60%;
}
.modal.open .content {
  right: 0;
}
.modal.open .content .category {
  top: 0;
}
.modal.open .content h2 {
  opacity: 1;
  margin-left: 0;
}
.modal.open .content h4 {
  top: 0;
  opacity: 1;
}
.modal.open .content .line {
  width: 90px;
  opacity: 1;
}
.modal.open .content p {
  opacity: 1;
}
.modal.open .content a {
  opacity: 1;
}

.trigger {
  position: absolute;
  top: 24px;
  right: 24px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 1px;
  text-decoration: none;
  color: #333;
}

* {
  box-sizing: border-box;
}

.drib {
  text-align: center;
  display: block;
  margin-top: 20px;
  color: #fff;
}
.drib .fa {
  color: #ea4c89;
}

body {
  background: #777;
  font-family: "Lato", sans-serif;
}

感谢您的帮助

【问题讨论】:

  • 看起来你的脚本在你包含 jquery 之前就已经运行了 - 所以$ 不会被定义,你应该在浏览器的控制台中看到一个错误告诉你这个。
  • 将带有模态部分的脚本块移动到页面底部,就在&lt;/body&gt;标签之前
  • 已经试过了,还是不行
  • 浏览器控制台出现什么错误?
  • alert(x); var x = 5; // uh, well, yeah, of course it doesn't work.

标签: javascript jquery html css


【解决方案1】:

在包含 jquery 之前,您不能使用 jquery,即:

<script type="text/javascript">
var modal = $('.modal');
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

必须是:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var modal = $('.modal');
</script>

关于放置这些的最佳位置仍有一些争论(在 &lt;head&gt;(可用于页面)或就在 &lt;/body&gt; 之前(因此内容加载更快)),只要它们是正确的顺序。

【讨论】:

  • “就像我说的那样,Freedomn-m 是对的 - 不知道为什么它应该被否决...跨度>
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 2021-11-21
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 2021-04-04
  • 1970-01-01
相关资源
最近更新 更多