我在页面index.html中有一个链接,我需要打开一个div,它位于名为index2.html的其他页面中,但我不能这样做。
我的代码是:
index.html中的链接
<a href="#modal" id="open-modal">Open Modal</a>
index2.html页面中的内容:
<div id="modal" class="style-modal">
<a id="close_modal" href="#" title="close modal"></a>
<div id="content-modal">
<p>Content here</p>
</div>
</div>
这是我在efect.js中的代码:
$(document).ready(function(){
$("#open_modal").click(function(){
$( '#modal' ).fadeIn();
$( '#bg-modal' ).fadeTo( 500, .5 );
});
$("#close_modal").click(function(){
$( '#modal, #bg-modal' ).fadeOut();
});
});
请帮帮我。
答案 0 :(得分:0)
尝试使用window.location.hash:
index.html中的链接:
<a href="http://example.com/index2.html#show-modal" id="open-modal">Open Modal</a>
index2.html页面中的内容:
<div id="modal" class="style-modal">
<a id="close_modal" href="#" title="close modal"></a>
<div id="content-modal">
<p>Content here</p>
</div>
</div>
这是我在efect.js中的代码:
function showModal() {
$( '#modal' ).fadeIn();
$( '#bg-modal' ).fadeTo( 500, .5 );
}
$(document).ready(function(){
$("#open_modal").on('click', showModal);
$("#close_modal").click(function(){
$( '#modal, #bg-modal' ).fadeOut();
});
if (window.location.hash == '#show-modal') {
showModal();
}
});
答案 1 :(得分:0)
如果您正在尝试将另一个页面的div加载到模态中,请尝试使用这样的AJAX:
小提琴:http://jsfiddle.net/hwVrN/1/
的 HTML 强>:
<a href="#modal" id="open-modal">Open Modal</a>
<div id="modal-container">
</div>
JS
$(document).ready(function(){
$("#open-modal").click(function(){
$('#modal-container').load('index2.html',function(){
$( '#modal' ).fadeIn();
$( '#bg-modal' ).fadeTo( 500, .5 );
});
});
$("#close-modal").click(function(){
$( '#modal, #bg-modal' ).fadeOut();
});
});