自动弹出javascript ...如何处理这段代码?

时间:2011-03-26 01:38:43

标签: php javascript jquery

我想做的是当用户转到此页面时,javascript表单将自动打开,用户无需单击某个按钮。

的index.html:

<script>

$(document).ready(function() {  

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect     
        $('#mask').fadeIn(1000);    
        $('#mask').fadeTo("slow",0.8);  

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();


        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000); 

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });     

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         

});

</script>

HTML:

<div id="boxes">

<div id="dialog" class="window">
Simple Modal Window | 
<a href="#"class="close"/>Close it</a>
</div>

<!-- Start of Login Dialog -->  
<div id="dialog1" class="window">
  <div class="d-header">


  <form action="process.php" method="get">

    <input type="text" name="name" id="name" value="NAME " onclick="this.value=''"/><br/>
    <input type="text"  name="email"  id="email" value="E-MAIL " onclick="this.value=''"/>
    <input type="text"  name="contact" id="contact" value="CONTACT " onclick="this.value=''"/> 

    <select  name="program" id="program" /> 
    <option value=""  selected >PROGRAM - Select - </option>
        <option value="MEDICINE" >MEDICINE </option>
        <option value="DENTAL" onclick="this.value=''">DENTAL </option>
        <option value="PHARMACY" onclick="this.value=''">PHARMACY  </option>
    </select>

    <select type="text"  name="country"  id="country" /> 
      <option value="" selected="selected">COUNTRY - Select - </option>
         <option value="RUSSIA " >RUSSIA  </option>
        <option value="INDONESIA " >INDONESIA  </option>
        <option value="INDIA " >INDIA   </option>
    </select>


    <select type="text"  name="scholarship"  id="scholarship"/> 
    <option value="" selected="selected">SCHOLARSHIP - Select - </option>
       <option value="FULL " onclick="this.value=''">FULL  </option>
       <option value="PARTIAL " onclick="this.value=''">PARTIAL   </option>
    </select>
  </div>
  <div class="d-blank"></div>
  <div class="d-login"><input type="submit"  value="submit" style="margin-top:210px; margin-left:-120px;"/></div>
</div>
<!-- End of Login Dialog -->  



<!-- Start of Sticky Note -->
<div id="dialog2" class="window">
  So, with this <b>Simple Jquery Modal Window</b>, it can be in any shapes you want! Simple and Easy to modify : ) <br/><br/>
<input type="button" value="Close it" class="close"/>
</form>
</div>
</div>

2 个答案:

答案 0 :(得分:1)

只需删除$('a[name=modal]').click(function(e) { ... })包装器 - 您也可以带走e.preventDefault(),因为没有任何事件可以阻止。

答案 1 :(得分:0)

只需触发其中一个链接上的点击事件即可。除了这一行之外,您不必编写任何新代码:

$(document).ready()
{
    // Add this last
    $('a[name=modal]').eq(0).click();
});

此代码会自动点击第一个a[name=modal],因此您无需修改​​和创建新代码只是以自动加载此内容。


尝试添加此代码:

$(document).ready()
{
    // Add this last
    $('a[href="#dialog1"]:eq(0)').click();
});