0

我在页面 index.html 中有一个链接,我需要打开一个位于另一个名为 index2.html 的页面中的 div,但我做不到。

我的代码是这样的:

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();
        });
    });

请帮我。

4

2 回答 2

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();
        }
    });
于 2013-05-24T00:18:09.197 回答
0

如果您尝试将 div 从另一个页面加载到模式中,请尝试像这样使用 AJAX:
Fiddle : 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();
        });
    });
于 2013-05-24T00:21:01.393 回答