2016-11-24 119 views
0

我是JS菜鳥,所以請耐心等待我。我正在一個有照片庫的網站上工作,當你點擊一張圖片時,它會打開一個模式。我已經制作了一個免費的模板來實現這一點。JS添加關閉按鈕模態

但是我添加了一個'close'圖標,但我不知道如何使它工作。你能幫我麼?

下面的代碼:

$(document).ready(function(){   
 
\t $('li img').on('click',function(){ 
 
\t \t var src = $(this).attr('src'); 
 
\t \t var img = '<img src="' + src + '" class="img-responsive"/>'; 
 
\t \t 
 
\t \t //start of new code new code 
 
\t \t var index = $(this).parent('li').index(); 
 

 
\t \t var html = ''; 
 
\t \t html += '<span class="close" style=padding:5px;>X</span>'; 
 
\t \t html += img;     
 
\t \t html += '<div style="height:25px;clear:both;display:block;">'; 
 
\t \t html += '<a class="controls next" href="'+ (index+2) + '">pros &raquo;</a>'; 
 
\t \t html += '<a class="controls previous" href="' + (index) + '">&laquo; prec</a>'; 
 
\t \t html += '</div>'; 
 
\t \t 
 
\t \t $('#myModal').modal(); 
 
\t \t $('#myModal').on('shown.bs.modal', function(){ 
 
\t \t \t $('#myModal .modal-body').html(html); 
 
\t \t \t //new code 
 
\t \t \t $('a.controls').trigger('click'); 
 
\t \t \t $('span.close').trigger('click'); 
 
\t \t }) 
 
\t \t $('#myModal').on('hidden.bs.modal', function(){ 
 
\t \t \t $('#myModal .modal-body').html(''); 
 
\t \t }); 
 
\t \t \t \t 
 
    }); \t 
 
}) 
 
      
 
$(document).on('click', 'a.controls', function(){ 
 
\t var index = $(this).attr('href'); 
 
\t var src = $('ul.row li:nth-child('+ index +') img').attr('src');    
 
\t 
 
\t $('.modal-body img').attr('src', src); 
 
\t 
 
\t var newPrevIndex = parseInt(index) - 1; 
 
\t var newNextIndex = parseInt(newPrevIndex) + 2; 
 
\t 
 
\t if($(this).hasClass('previous')){    
 
\t \t $(this).attr('href', newPrevIndex); 
 
\t \t $('a.next').attr('href', newNextIndex); 
 
\t }else{ 
 
\t \t $(this).attr('href', newNextIndex); 
 
\t \t $('a.previous').attr('href', newPrevIndex); 
 
\t } 
 
\t 
 
\t var total = $('ul.row li').length + 1; 
 
\t //hide next button 
 
\t if(total === newNextIndex){ 
 
\t \t $('a.next').hide(); 
 
\t }else{ 
 
\t \t $('a.next').show() 
 
\t }    
 
\t //hide previous button 
 
\t if(newPrevIndex === 0){ 
 
\t \t $('a.previous').hide(); 
 
\t }else{ 
 
\t \t $('a.previous').show() 
 
\t } 
 
\t 
 
\t 
 
\t return false; 
 
});

預先感謝您:-)

+0

如果你可以發佈您的代碼的JSBin或的jsfiddle您將很快得到解決方案。 :) – Mihailo

+0

我很無聊,所以我做了[這](http://jsbin.com/sizuzas/edit?html,js,output)。如果您覺得有用,我會提供更多信息。 – Mihailo

回答

0

請使用event委派技術click處理程序附加到你的親密元素。這是因爲你正在通過腳本生成模態的DOM,即它是一個動態生成的HTML。你可以試試下面的代碼,而不是你的代碼:

$(document).on("click", ".close", function(){ 
    // call 'close' method on nearest kendoWindow 
    $(this).closest("[data-role=window]").data("kendoWindow").close(); 
    // the above is equivalent to: 
    //$(this).closest(".k-window-content").data("kendoWindow").close(); 
}); 
+0

發現錯誤!還有另一個叫做「close」的css類,它引發了一場衝突......傻了我。現在你的方法奏效了。謝謝。 – guaranito93

-1

爲簡化器乾草使用的框架作業兄弟

從w3school http://www.w3schools.com/howto/howto_css_modals.asp

<!-- Trigger/Open The Modal --> 
<button id="myBtn">Open Modal</button> 

<!-- The Modal --> 
<div id="myModal" class="modal"> 

    <!-- Modal content --> 
    <div class="modal-content"> 
    <span class="close">x</span> 
    <p>Some text in the Modal..</p> 
    </div> 

</div> 




// Get the modal 
var modal = document.getElementById('myModal'); 

// Get the button that opens the modal 
var btn = document.getElementById("myBtn"); 

// Get the <span> element that closes the modal 
var span = document.getElementsByClassName("close")[0]; 

// When the user clicks on the button, open the modal 
btn.onclick = function() { 
    modal.style.display = "block"; 
} 

// When the user clicks on <span> (x), close the modal 
span.onclick = function() { 
    modal.style.display = "none"; 
} 

// When the user clicks anywhere outside of the modal, close it 
window.onclick = function(event) { 
    if (event.target == modal) { 
     modal.style.display = "none"; 
    } 
} 






<!-- Trigger/Open The Modal --> 
<button id="myBtn">Open Modal</button> 

<!-- The Modal --> 
<div id="myModal" class="modal"> 

    <!-- Modal content --> 
    <div class="modal-content"> 
    <span class="close">x</span> 
    <p>Some text in the Modal..</p> 
    </div> 

</div> 
Step 2) Add CSS: 

Example 
/* The Modal (background) */ 
.modal { 
    display: none; /* Hidden by default */ 
    position: fixed; /* Stay in place */ 
    z-index: 1; /* Sit on top */ 
    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.4); /* Black w/ opacity */ 
} 

/* Modal Content/Box */ 
.modal-content { 
    background-color: #fefefe; 
    margin: 15% auto; /* 15% from the top and centered */ 
    padding: 20px; 
    border: 1px solid #888; 
    width: 80%; /* Could be more or less, depending on screen size */ 
} 

/* The Close Button */ 
.close { 
    color: #aaa; 
    float: right; 
    font-size: 28px; 
    font-weight: bold; 
} 

.close:hover, 
.close:focus { 
    color: black; 
    text-decoration: none; 
    cursor: pointer; 
} 
+0

我檢查過,但我不知道如何將其應用於我的案例。檢查我提供的JS代碼,看看你是否明白如何做到這一點:-) – guaranito93