나는 자바 스크립트를 전혀 모른다. 부트 스트랩 문서에 따르면
javascript를 통해 모달을 호출하십시오. $ ( '# myModal'). modal (options)
페이지로드에서 이것을 호출하는 방법에 대한 단서가 없습니다. 부트 스트랩 페이지에서 제공된 코드를 사용하여 요소 클릭시 모달을 성공적으로 호출 할 수 있지만 페이지로드시 즉시로드하려고합니다.
나는 자바 스크립트를 전혀 모른다. 부트 스트랩 문서에 따르면
javascript를 통해 모달을 호출하십시오. $ ( '# myModal'). modal (options)
페이지로드에서 이것을 호출하는 방법에 대한 단서가 없습니다. 부트 스트랩 페이지에서 제공된 코드를 사용하여 요소 클릭시 모달을 성공적으로 호출 할 수 있지만 페이지로드시 즉시로드하려고합니다.
답변:
load문서 헤드 섹션의 jQuery 이벤트 내에서 페이지로드에서 호출하려는 모달을 래핑하면 다음 과 같이 팝업됩니다.
JS
<script type="text/javascript">
$(window).on('load',function(){
$('#myModal').modal('show');
});
</script>
HTML
<div class="modal hide fade" id="myModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<a href="#" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>
</div>
</div>
다음과 같은 링크로 호출하여 페이지 내에서 모달을 호출 할 수 있습니다.
<a class="btn" data-toggle="modal" href="#myModal" >Launch Modal</a>
$(document).ready( ... 입니다. 윈도우로드 이벤트에서만 작동합니다 : $(window).load( ... .
$(document).ready( ...사용자가 무언가를 클릭 할 때 동적으로 추가되는 모달의 MVC PartialView 에이 스크립트를 저장할 때만 작동 하므로 어쩌면 그럴 수 있습니다. 아이러니하게도, 하지 말아야 할 것에 대한 당신의 경고는 그것이 나를 위해 일하는 방법을 알아낼 수있게 해주었습니다. 감사합니다? :)
javascript모달을 표시 할 필요가 없습니다
가장 간단한 방법은 "hide"를 "in"으로 바꾸는 것입니다.
class="modal fade hide"
그래서
class="modal fade in"
onclick = "$('.modal').hide()"버튼 닫기에 추가해야합니다 .
추신 : 가장 좋은 방법은 jQuery 스크립트를 추가하는 것입니다.
$('.modal').modal('show');
다음을 추가하십시오.
class="modal show"
작업 예
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal show" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
onclick = "$('.modal').removeClass('show').addClass('fade');"
이 실행 가능한 코드를 사용해보십시오.
$(window).load(function()
{
$('#myModal').modal('show');
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
자세한 내용은 여기를 참조하십시오 .
당신의 완전한 답이 있다고 생각하십시오.
필자의 경우 jQuery를 추가하여 모달을 표시하지 못했습니다.
$(document).ready(function() {
$('#myModal').modal('show');
});
모달의 속성이 aria-hidden = "true"였기 때문인 것 같습니다.
<div class="modal fade" aria-hidden="true" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
위의 jQuery뿐만 아니라 해당 속성을 제거해야했습니다.
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
모달 클래스를에서 (으) modal fade로 변경하려고 시도했지만 modal fade in작동하지 않습니다. 또한로부터 클래스를 변경 modal fade하려면 modal show폐쇄 될 수있는에서 모달 멈췄다.
javascript로 모달을 초기화 하지 않고 예제 를 찾을 수 없으므로 페이지로드시 javascript 지연$('#myModal').modal('show') 없이 구현할 수있는 방법에 대한 제안이 있습니다 .
<div class="modal in" id="MyModal" tabindex="-1" role="dialog" style="display: block; padding-right: 17px;">
클래스와 스타일로 몸을 편집하십시오.
<body class="modal-open" style="padding-right:17px;">
모달 배경 div 추가
<div class="modal-backdrop in"></div>
스크립트 추가
$(document).ready(function() {
$('body').css('padding-right', '0px');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
$('#MyModal').modal('show'); });
일어날 일은 모달의 html이 자바 스크립트없이 페이지로드에로드 될 것입니다 (지연 없음). 이 시점에서 모달을 닫을 수 없으므로 모든 것이로드 될 때 모달을 올바르게로드하기 위해 document.ready 스크립트가 있습니다. 실제로 사용자 지정 코드를 제거한 다음 .modal ( 'show')를 사용하여 모달 (다시)을 초기화합니다.
다른 사람들이 언급했듯이 display : block으로 모달을 만듭니다.
<div class="modal in" tabindex="-1" role="dialog" style="display:block">
...
배경을 페이지의 아무 곳에 나 배치하십시오. 반드시 페이지 하단에있을 필요는 없습니다.
<div class="modal-backdrop fade show"></div>
그런 다음 대화 상자를 다시 닫으려면 다음을 추가하십시오.
<script>
$("button[data-dismiss=modal]").click(function () {
$(".modal.in").removeClass("in").addClass("fade").hide();
$(".modal-backdrop").remove();
});
</script>
도움이 되었기를 바랍니다
부트 스트랩 3 및 jQuery (2.2 및 2.3)로 테스트
$(window).on('load',function(){
$('#myModal').modal('show');
});
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><i class="fa fa-exclamation-circle"></i> //Your modal Title</h4>
</div>
<div class="modal-body">
//Your modal Content
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
user2545728 및 Reft 답변 외에도 자바 스크립트는 없지만modal-backdrop in
추가해야 할 3 가지
modal-backdrop in.modal 클래스 앞의 클래스가있는 divstyle="display:block;" .modal 클래스로fade in .modal 클래스와 함께예
<div class="modal-backdrop in"></div>
<div class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="channelModal" style="display:block;">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="channelModal">Welcome!</h4>
</div>
<div class="modal-body" style="height:350px;">
How did you find us?
</div>
</div>
</div>
</div>
2020 업데이트-부트 스트랩 4
Bootstrap 4의 모달 마크 업이 약간 변경되었습니다. 페이지로드시 모달을 열고 선택적으로 모달의 표시를 지연시키는 방법은 다음과 같습니다.
$(window).on('load',function(){
var delayMs = 1500; // delay in milliseconds
setTimeout(function(){
$('#myModal').modal('show');
}, delayMs);
});
<div class="modal fade" id="myModal">
<div class="modal-dialog modal-lg modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">My Modal</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary mx-auto" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
부트 스트랩 3에서는 js를 통해 모달을 초기화해야하며 페이지로드 순간에 모달 마크 업이 페이지에 있으면 모달이 표시됩니다.
이를 방지 show: false하려면 모달을 초기화하는 옵션을 사용하십시오 . 이 같은:
$('.modal').modal({ show: false })
jquery.js더 빠른 페이지로드 를 위해 지연 상태를 유지할 수 있습니다 . 그러나 jquery.js지연되면 $(window).load작동하지 않을 수 있습니다. 그럼 당신은 시도 할 수 있습니다
setTimeout(function(){$('#myModal').modal('show');},3000);
페이지가 완전히로드 된 후 모달이 팝업됩니다 (jquery 포함)
부트 스트랩이 무효화되고 기능이 손실되는 것을 피하려면 일반 시작 버튼을 만들고 ID를 제공 한 다음 jquery를 사용하여 '클릭'하십시오.
<button type="button" id="btnAnouncement" class="btn btn-primary" data-toggle="modal" data-target="#modalAnouncement">
See what´s new!
</button>
jQuery 트리거 :
$(document).ready(function () {
$('#btnAnouncement').click();
)}
도움이 되길 바랍니다. 건배!
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('#myModal').modal('show');
setTimeout(function(){
$('#myModal').modal('hide');
}, 5000);
});
</script>
<style>
.loader {
border: 13px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 100px;
height: 100px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
}
/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.modal-dialog{
width: 150px;
margin: 10% auto;
}
.modal-content{
background-color: transparent;
border: 0;
border-radius: 0;
outline: 0;
box-shadow: none;
}
</style>
</head>
<body>
<div class="container">
<!-- Modal -->
<div class="modal fade" data-backdrop="static" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<div class="loader"></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>