弹出窗口无法正常工作

时间:2013-03-10 05:52:23

标签: javascript jquery html

我想在" popup"时弹出一个窗口。按钮被按下作为我的学校Javascript作业的一部分,但无法弄清楚为什么我的代码不起作用。我已将弹出按钮分配给jQuery选择器,但仍然无法打开。

<html>
<head>
<title>Adam Ginther's Pop-up Window</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="panel">
Congrats! You won!
</div>
<div id="centreblock">
<h1>WOW! AMAZING!!!</h1>
<img src="images/ipad.gif" width="100%" height="250">
<p>Congratulations! You won a free iPad! Enter your name, address, and phone number to have it delivered now.
<form action="">
<br>
<label for="firstName">First Name: </label>
<input type="text" size="12" id="firstName" name="firstName">
<br>
<br>
<label for ="lastName">Last Name: </label>
<input type="text" size"12" id="lastName" name="lastName">
<br>
<br>
<label for="address">Address: </label>
<input type="text" size="12" id="address" name="address">
<br>
<br>
<label for="phoneNum">Phone Number: </label>
<input type="text" size="12" id="phoneNum" name="phoneNum">
</form>
<br>
<a href="#" id="popup"><br>REDEEM THIS SPECTACULAR OFFER NOW!</a>
<br>
</div>
<script src="http//code.jquery.com/jquery-latest.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>


//script.js
var new_win;
var firstName;
var lastName;
var address;
var phoneNumber;

function newWindow() { 
    firstName = $('#firstName').val();
    if (firstName !='') {
    window.open('popup.html');
    } else {
        $('#firstName').addClass('error');
    };
    $(function() {
        $('#popup').click(function() {
            newWindow();
        });
    });
};

1 个答案:

答案 0 :(得分:1)

试试这个:

<html>
<head>
<title>Adam Ginther's Pop-up Window</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/style.css">

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="js/script.js"></script>
<script language="javascript">


//script.js
var new_win;
var firstName;
var lastName;
var address;
var phoneNumber;

function popitup(url) {
    newwindow=window.open(url,'name','height=200,width=150');
    if (window.focus) {newwindow.focus()}
    return false;
}

function newWindow() { 
    firstName = $('#firstName').val();
    if (firstName !='') {
    popitup('popup.html');
    } else {
        $('#firstName').addClass('error');
    };
};

    $(document).ready(function(){
        $('#popup').click(function() {
            newWindow();
        });
    });
</script>
</head>
<body>
<div id="panel">
Congrats! You won!
</div>
<div id="centreblock">
<h1>WOW! AMAZING!!!</h1>
<img src="images/ipad.gif" width="100%" height="250">
<p>Congratulations! You won a free iPad! Enter your name, address, and phone number to have it delivered now.
<form action="">
<br>
<label for="firstName">First Name: </label>
<input type="text" size="12" id="firstName" name="firstName">
<br>
<br>
<label for ="lastName">Last Name: </label>
<input type="text" size"12" id="lastName" name="lastName">
<br>
<br>
<label for="address">Address: </label>
<input type="text" size="12" id="address" name="address">
<br>
<br>
<label for="phoneNum">Phone Number: </label>
<input type="text" size="12" id="phoneNum" name="phoneNum">
</form>
<br>
<a href="javascript:void(0)" id="popup"><br>REDEEM THIS SPECTACULAR OFFER NOW!</a>
<br>
</div>
</body>
</html>

请注意:

  • jquery版本已过时
  • 在javascript函数中进行了一些更改

我希望这可能会有所帮助。您可以根据您的流量和要求进行编辑。