簡體   English   中英

單擊按鈕時在彈出窗口中顯示圖像

[英]Show image in a popup when I click button

我有一個按鈕,當我單擊此按鈕時,我需要在彈出窗口中顯示圖像。

為什么這段代碼不起作用? 我嘗試在onclick事件中使用 window.open 但它不起作用

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

我的錯誤在哪里?

你可以使用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>

您在 onclick 中缺少"並為新頁面添加_blank 。使用'引號將參數與 in 函數一起傳遞

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

您使用的屬性和函數是錯誤的:

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

總是引用一個屬性值,就像你對typevalue所做的那樣。
而且window.open函數需要一個路徑,不需要寫“src=”。

MDN - window.open()

試試這個:

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希望有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM