JavaScript无法在aspx C#网页中使用

时间:2016-08-30 01:54:57

标签: javascript c# asp.net

当我把它放在脚本标记内的asps网页中时,我的JavaScript无效。代码也在图像和代码中。

它实际上是一个打开lightBox/fancyBox图像的模型框,但它只打开模型框1秒钟,但它在普通的html页面中工作

ASP and HTML code

JavaScript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="img-model.aspx.cs" >Inherits="webpages_img_model" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>model box</title>
 <style>
       /* The Modal (background) */
       .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 100px; /* Location of the box */
            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 */
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
             border: 1px solid #888;
            width: 60%;
            height:auto;
        }

        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
         .clear {
            clear:both;
         }

            .close:hover,
            .close:focus {
                color: #000;
                text-decoration: none;
                 cursor: pointer;
            }
           .modal-content .p1 {
               float:left;
              border-right:1px;
              width:50%;
            }
           .modal-content .p2 {
               float:right;
               width:50%;

            }
           #img {
          position:relative;
          width:400px;
          height:400px;

      }
     #img #mm{
          position:absolute;
          top:0px;
          right:0px;
          z-index: 16;
      }
      #img img{
          width:100%;
          height:auto;
      }
    </style>


 </head>
 <body>

     <form id="form1" runat="server">

          <h2>Modal Example</h2>
     <div id="img">
         <a href="">
             <img src="../images/1pic.jpg" width="400px" height="400px" />
             <span id="mm"><button id="d">MM</button> </span>
         </a>
     </div>
     <!-- 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">×</span>

                      <div class="p1" >
                          <img src="../images/1pic.jpg" height="100%" width="80%" > />
                     </div>

                      <div class="p2">
                 <p>Some text in the Modal..</p> <br />
        <p>Some text in the Modal..</p> <br />
         <p>Some text ithe Modal..</p> <br />
        <p>Some text in the Modal..</p> <br />
            <input id="Button1" type="button" value="button" />
                      </div>
             <div class="clear"></div>
         </div>

    </div>
          <script type="text/javascript">
              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 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";
                  }
              }
         </script>



         </form>
 </body>
 </html>

1 个答案:

答案 0 :(得分:0)

As I commented your issue when the user clicks on the Open Modal button it submits the form which means essentially reloaded the page. What you need to do is return false on your btn.onclick = function () function. For simplicity I have posted all the code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="img-model.aspx.cs" >Inherits="webpages_img_model" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>model box</title>
    <style>
        /* The Modal (background) */
        .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 100px; /* Location of the box */
            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 */
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 60%;
            height:auto;
        }

        /* The Close Button */
        .close {
            color: #aaaaaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }
        .clear {
            clear:both;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }
        .modal-content .p1 {
            float:left;
            border-right:1px;
            width:50%;
        }
        .modal-content .p2 {
            float:right;
            width:50%;
        }
        #img {
            position:relative;
            width:400px;
            height:400px;
        }
        #img #mm{
            position:absolute;
            top:0px;
            right:0px;
            z-index: 16;
        }
        #img img{
            width:100%;
            height:auto;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Modal Example</h2>
        <div id="img">
            <!-- added # -->
            <a href="#">
                <!-- I would set the dimensions width and height in CSS -->
                <img src="../images/1pic.jpg" width="400" height="400" />
                <span id="mm">
                    <button id="d">MM</button> </span>
            </a>
        </div>
        <!-- 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">×</span>
                <div class="p1" >
                    <img src="../images/1pic.jpg" height="100" width="80" />
                </div>
                <div class="p2">
                    <p>Some text in the Modal..</p> <br />
                    <p>Some text in the Modal..</p> <br />
                    <p>Some text ithe Modal..</p> <br />
                    <p>Some text in the Modal..</p> <br />
                    <input id="Button1" type="button" value="button" />
                </div>
                <div class="clear">
                    &nbsp;
                </div>
            </div>
        </div>

        <script type="text/javascript">
            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 the button, open the modal
            btn.onclick = function () {
                modal.style.display = "block";
                /* added to prevent the page to submitting */
                return false;
            }

            // 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
            /* got rid of the $window and just replaced it with window */
            window.onclick = function (event) {
                if (event.target == modal) {
                    modal.style.display = "none";
                }
            }
        </script>
    </form>
</body>
</html>

I made some other changes as well to the html and javascript, see inline comments