2016-11-24 118 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