简体   繁体   English

使不可见的div在点击时可见

[英]Making an invisible div turn visible on click not working

I have tried but can't seem to figure out how to debug this. 我已经尝试过,但是似乎无法弄清楚如何调试它。 The div should be invisible until the user clicks on the "REDEEM THIS OFFER NOW!" 在用户单击“立即兑换此优惠!”之前,div应该是不可见的。 button. 按钮。 The "panel" div is supposed to appear over the content. “面板” div应该出现在内容上方。 If the user does not enter anything into the text field it should turn red. 如果用户未在文本字段中输入任何内容,则应变为红色。

<div id="centreblock">
            <h1>WOW! AMAZING!!!</h1>
            <img src="images/ipad.gif" width="100%" height="250">
            <h2>Congratulations! You won a free iPad! Enter your name, address, and phone number        to have it delivered now.</h2>
    <form action="">
    <label for="firstName">First Name: </label>
    <input type="text" size="12" id="firstName" name="firstName" maxlength="30">
    <br>
    <p id="output"></p>
    <input type="button" id="popup" value="REDEEM THIS OFFER NOW!">
    <br>
    </form>
    </div>
    <div id="panel">
        <h1 id="output-inside"></h1>
    </div>
    <script src="http//code.jquery.com/jquery-latest.min.js"></script>
    <script src="js/script.js"></script>

Javascript Java脚本

//script.js
var firstName;

function newPanel() {
    $('#panel').show();
    $('#output-inside').text('Congrats ' + firstName + ', your iPad will be delivered to you in the next century') 
}

function panelCheck() {
    firstName = $('#firstName').val();
    if (firstName != '') {
        $('#firstName').removeClass('error');
        $('#output').text('');
        newPanel();
    } else {
        $('#firstName').addClass('error').focus();
        $('#output').text('We need your name');
    }
}
$(function() {
    $('#panel').hide();
    $('#popup').click(function() {
        panelCheck();
    });
    $('#firstName').keypress(function(e) {
        if (e.keyCode === 13) {
            panelCheck();
        }
    });
    $('#close-panel').click(function() {
        $('#panel').hide();
    });
});

why don't you try something like this ? 你为什么不尝试这样的事情?

<div id="centreblock">      
<form action="" id="panel2">
    <h1>WOW! AMAZING!!!</h1>
    <h2>Congratulations! You won a free iPad! Enter your name, address, and phone number to have it delivered now.</h2>
    <label for="firstName">First Name: </label>
    <input type="text" size="12" id="firstName" name="firstName" maxlength="30" />
    <br />
    <p id="output"></p>
    <input type="button" id="popup" value="REDEEM THIS OFFER NOW!" />
    <br />
</form>
<div id="panel">
    <h1 id="output-inside"></h1>
</div>

function newPanel() {
$('#panel').show();
$('#output-inside').text('Congrats ' + firstName + ', your iPad will be delivered to you in the next century');
$('#panel2').hide();}

here's a working demo http://jsfiddle.net/FJrKq/3/ 这是一个有效的演示http://jsfiddle.net/FJrKq/3/

I've edited the order of some of your elements and also hidden some of them once the panel is displayed. 我已经编辑了某些元素的顺序,并且在显示面板后也隐藏了其中的一些元素。 (also I've removed some of the text so it would be easier to see and quickly edit the page) (我也删除了一些文本,这样可以更轻松地查看和快速编辑页面)

Write your JavaScript code into: 将您的JavaScript代码写入:

$('document').ready(function(){
   // your code
});

Write your functions outside of this scope. 在此范围之外编写函数。

Add

position:absolute 

to "#panel" in style block. 样式块中的“ #panel”。

You should be use in your start of jQuery line 您应该在jQuery行的开头使用

$('document').ready(function(){
   // your code here

var firstName;

function newPanel() {
    $('#panel').show();
    $('#output-inside').text('Congrats ' + firstName + ', your iPad will be delivered to you in the next century') 
}

function panelCheck() {
    firstName = $('#firstName').val();
    if (firstName != '') {
        $('#firstName').removeClass('error');
        $('#output').text('');
        newPanel();
    } else {
        $('#firstName').addClass('error').focus();
        $('#output').text('We need your name');
    }
}
$(function() {
    $('#panel').hide();
    $('#popup').click(function() {
        panelCheck();
    });
    $('#firstName').keypress(function(e) {
        if (e.keyCode === 13) {
            panelCheck();
        }
    });
    $('#close-panel').click(function() {
        $('#panel').hide();
    });
});

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM