单击按钮时在弹出窗口中显示图像

时间:2017-06-07 09:51:27

标签: javascript html css

我有一个按钮,当我点击此按钮时,我需要在弹出窗口中显示图像。

为什么这段代码不起作用?我尝试在onclick事件中使用window.open,但它不起作用

<input type="button" value="Mostra Immagine" onclick=window.open(src="../img/mypicture.jpg")>

我的错误在哪里?

4 个答案:

答案 0 :(得分:0)

您错过了onclick中的",并使用_blank添加新页面。使用'引号在函数中传递参数

<input type="button" value="Mostra Immagine" onclick="window.open('../img/mypicture.jpg','_blank')">

答案 1 :(得分:0)

您使用的属性和功能错误:

onclick="window.open('../img/mypicture.jpg');"

始终引用属性值,就像您对typevalue所做的那样 window.open函数需要一个路径,你不需要写“src =”。

MDN - window.open()

答案 2 :(得分:0)

您可以使用 fancybox 来实现。

a {
  color: #FFFFFF;
  text-decoration: none;
}

.primary-btn {
  background-image: -moz-linear-gradient(0deg, #235ee7 0%, #4ae7fa 100%);
  background-image: -webkit-linear-gradient(0deg, #235ee7 0%, #4ae7fa 100%);
  background-image: -ms-linear-gradient(0deg, #235ee7 0%, #4ae7fa 100%);
}

.primary-btn {
  line-height: 36px;
  padding-left: 30px;
  padding-right: 30px;
  border-radius: 25px;
  border: none;
  color: #fff;
  display: inline-block;
  font-weight: 500;
  position: relative;
  -webkit-transition: all 0.3s ease 0s;
  -moz-transition: all 0.3s ease 0s;
  -o-transition: all 0.3s ease 0s;
  transition: all 0.3s ease 0s;
  cursor: pointer;
  text-transform: uppercase;
  position: relative;
}

.primary-btn {
  color: #fff;
  border: 1px solid #fff;
  -webkit-transition: all 0.3s ease 0s;
  -moz-transition: all 0.3s ease 0s;
  -o-transition: all 0.3s ease 0s;
  transition: all 0.3s ease 0s;
}

.primary-btn:hover {
  background: transparent;
  color: #235ee7;
  border-color: #235ee7;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.0/jquery.fancybox.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.0/jquery.fancybox.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.0/jquery.fancybox.min.css">
<a data-fancybox="gallery" class="primary-btn" href="//source.unsplash.com/user/erondu/300x300">Demo</a>

答案 3 :(得分:-1)

试试这个:

HTML:

<div class="popup" onclick="myFunction()">Click me!
  <span class="popuptext" id="myPopup">Popup text...</span>
</div>

CSS:

/* Popup container */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

/* The actual popup (appears on top) */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class when clicking on the popup container (hide and show the popup) */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}

JavaScript的:

<script>
// When the user clicks on <div>, open the popup
function myFunction() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}
</script>

您可以在此处阅读更多内容https://www.w3schools.com/howto/howto_js_popup.asp 希望有用