doujiao7520 2014-10-06 20:24
浏览 54
已采纳

使用jQuery onClick按钮淡入PHP脚本

I have a PHP script labeled first_page.php. On that page, I currently have a div that looks like this:

<div id="status">
  <h3>To view a list of all rooms statuses, select the link below.</h3>
  <a href="response_data.php" class="button">Show Status</a>
</div>

And links to the correct page, response_data.php. What i'd really like to have instead, is a button that is on the first_page.php, and when that button is clicked, have it load the response_data.php page with .fadeIn().

I've tried to get this jQuery script working, with no luck. Here is what I have tried. I've changed my html to look like this:

<button id="button">Click here to show data</button>
  <div id="data" style="display: none;"> 
<?php include 'response_data.php' ?>
</div>


$('#button').click(function() {
  $('#data').fadeIn(1000);
});

Above, I have added a button and a div that I wanted to fadeIn. The div holds the php script, so I wanted the div to fade the php script in. I set the data CSS to display none. When I click the button, nothing happens. It actually works, but the div data fade's in without having had the button clicked. Then the button remains. I'd like it to not auto click, and also somehow hide the button after the first click.

  • 写回答

1条回答 默认 最新

  • douwan2664 2014-10-06 20:40
    关注

    The jQuery part is just fine. I highly suggest to check that your div#data actually has some text in it.

    To do so, you can try something like:

    console.log($('#data').text().length > 0 ? 'Text found' : 'Could not find text');
    

    Also, to hide the button after it was clicked simply use the hide() method:

    $('#button').on('click', function() {
    
        $('#data').fadeIn(1000);
        $(this).hide();
    
    });
    

    Note: You can also use the fadeOut() method or the remove() method if you wish to remove the button from the DOM.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器