【问题标题】:How to stop property onclick undefined如何停止属性onclick undefined
【发布时间】:2017-07-06 18:45:15
【问题描述】:

我按照教程在我的网站上添加了模态图像。 https://www.w3schools.com/howto/howto_css_modal_images.asp

但是由于我有多张图片,所以我按照这个人在这个问题上所说的 Modal image galleries - multiple images

还是不行。

这是我的 javascript: 它告诉我“未捕获的类型错误:无法设置未定义的属性‘onclick’” 对于我的 var "span"

var modal = document.getElementById('myModal');
var img = $('.myImg');
var modalImg = $("#img01");
var captionText = document.getElementById("caption");
$('.myImg').click(function(){
modal.style.display = "block";
var newSrc = this.src;
modalImg.attr('src', newSrc);
captionText.innerHTML = this.alt;
});
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}

这是我用脚本生成的 html:

<script id="mapListDetailTemplate" type="text/x-jQuery-tmpl">
<div class="result_item_detail">
<a href="#" class="map_details_close"><img src="src/close.png" alt="Close"></a>
<h3>${name}</h3>
<div class="result_item_detail_info">
    <div class="info">
        <ul class="list-unstyled">
            <li><b>Adresse</b>: ${address}</li>
        </ul>
    </div>
</div>
<div class="result_item_detail_info">
<h4>Description</h4>
${about}
</div>
<div class="result_item_detail_info">
<h4>Image</h4>
    <div class="slider_container">
        <div style="display: inline-block;">
              <img class="myImg" src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg" alt="Trolltunga, Norway" width="300" height="200"/>
        </div>
    </div>
</div>
<div class="result_item_detail_info">
<h4>Aujourdhui</h4>
    <div id="pano"> </div>
</div>
</div>
<div id="myModal" class="modal">
<span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
</script>

这里是 CSS

/* Style the Image Used to Trigger the Modal */
.myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}

.myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}

/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}

/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}

@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}

/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}

.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
    width: 100%;
}
}

你知道我为什么会出现这个错误吗?是不是因为我的 html 在脚本中??

【问题讨论】:

  • 是模态的,加载了.close跨度,然后定义了span.onclick?看起来您将 onclick 添加到 .close 跨度两次 - 内联和 span.onclick。内联应该是你所需要的。
  • 好的,我已经没有这个错误信息了,但它仍然不起作用>
  • 还有其他错误吗?你是什​​么意思它不起作用?如果您可以分享您的 CSS,我们将拥有完整的代码,以便我们提供更多帮助。
  • 我在控制台中没有更多错误,我通过编辑我的帖子来分享 CSS
  • 您是在模板加载后加载脚本吗??

标签: javascript


【解决方案1】:

如 cmets 中所述,HTML 结构似乎是模态中的模态,这可能是 UX/UI 问题,但可以解决。忽略这一点,这是一个基本的工作示例。与您所拥有的相比有一些变化。

  • 由于您使用的是模板,因此需要加载 jQuery 模板脚本
  • 然后需要在模板上调用.tmpl(),以及一些数据
  • onClick 需要在模板创建后添加

$(document).ready(function() {
  var items = [
    {name: 'Name 1', address: 'Address 1', about: 'About 1'}
  ];
  
  // Render the item using the template
  $("#mapListDetailTemplate").tmpl(items).appendTo("#container");

  var modal = document.getElementById('myModal');
  var modalImg = $("#img01");
  var captionText = document.getElementById("caption");
  $('.myImg').on('click', function() {
    modal.style.display = "block";
    var newSrc = this.src;
    modalImg.attr('src', newSrc);
    captionText.innerHTML = this.alt;
  });
  
  
});
/* Style the Image Used to Trigger the Modal */

.myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

.myImg:hover {
  opacity: 0.7;
}


/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.9);
  /* Black w/ opacity */
}


/* Modal Content (Image) */

.modal-content {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
}


/* Caption of Modal Image (Image Text) - Same Width as the Image */

#caption {
  margin: auto;
  display: block;
  width: 80%;
  max-width: 700px;
  text-align: center;
  color: #ccc;
  padding: 10px 0;
  height: 150px;
}


/* Add Animation - Zoom in the Modal */

.modal-content,
#caption {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    -webkit-transform: scale(0)
  }
  to {
    -webkit-transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}


/* The Close Button */

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: #bbb;
  text-decoration: none;
  cursor: pointer;
}


/* 100% Image Width on Smaller Screens */

@media only screen and (max-width: 700px) {
  .modal-content {
    width: 100%;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<div id="container"></div>
<script id="mapListDetailTemplate" type="text/x-jQuery-tmpl">
  <div class="result_item_detail">
    <a href="#" class="map_details_close"><img src="src/close.png" alt="Close"></a>
    <h3>${name}</h3>
    <div class="result_item_detail_info">
      <div class="info">
        <ul class="list-unstyled">
          <li><b>Adresse</b>: ${address}</li>
        </ul>
      </div>
    </div>
    <div class="result_item_detail_info">
      <h4>Description</h4>
      ${about}
    </div>
    <div class="result_item_detail_info">
      <h4>Image</h4>
      <div class="slider_container">
        <div style="display: inline-block;">
          <img class="myImg" src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg" alt="Trolltunga, Norway" width="300" height="200" />
        </div>
      </div>
    </div>
    <div class="result_item_detail_info">
      <h4>Aujourdhui</h4>
      <div id="pano"> </div>
    </div>
  </div>
  <div id="myModal" class="modal">
    <span class="close" onclick="document.getElementById('myModal').style.display='none'">&times;</span>
    <img class="modal-content" id="img01">
    <div id="caption"></div>
  </div>
</script>

【讨论】:

  • 非常感谢布雷特 :)
猜你喜欢
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多