简体   繁体   English

使用JavaScript原型自动隐藏DIV

[英]Automatically Hide DIV using javascript prototype

Before I begin, I'm using PHP and JS lib Prototype to handle Ajax in my code. 在开始之前,我将使用PHP和JS lib Prototype处理代码中的Ajax。

So my problem is the following: I'm using the following function to load a php file into a target DIV 所以我的问题如下:我正在使用以下函数将php文件加载到目标DIV中

   function ajaxUpdater(id, url)
{
    new Ajax.Updater('targetDiv', 'data.php', {asynchronous: true});
}

using the onClick function within a button, I grab the contents of data.php and display it in a DIV with the id of 'targetDiv'. 使用按钮内的onClick函数,我抓取了data.php的内容,并将其显示在ID为'targetDiv'的DIV中。

the problem is this. 问题是这样的。

There are certain things within data.php that i want to have hidden and only shown when an event is triggered. 我想隐藏data.php中的某些内容,并且仅在触发事件时才显示。 I've been trying loads of different solutions, but nothing seems to work. 我一直在尝试各种解决方案,但是似乎没有任何效果。 (just to add to the confusion, functions work when data.php is opened individually, but not when data.php is loaded using my ajax function. (只是增加了混乱,当单独打开data.php时,函数起作用,但是当使用我的ajax函数加载data.php时,函数不起作用。

Any help or clues or anything will be much appreciated! 任何帮助或提示或任何东西将不胜感激!

Hiroki, 弘树

I'd suggest passing some a parameter with your Ajax method and using some logic in data.php to pick and choose what data to send back. 我建议您通过Ajax方法传递一些参数,并在data.php中使用一些逻辑来选择要发送回的数据。 Here's an example of how I pass parameters with my prototype calls. 这是我如何通过原型调用传递参数的示例。

new Ajax.Updater('targetDiv', 'data.php', { parameters: { myParam1: 'hello', myParam2: 'world'} });

The go into your data.php file to create some logic. 进入data.php文件以创建一些逻辑。 Note that by default, prototype's method to send params is POST, but you can always change that by declaring method: 'get' in the same Ajax.Updater call, like so: 请注意,默认情况下,prototype的发送参数的方法是POST,但是您始终可以通过在同一Ajax.Updater调用中声明method: 'get'来更改它,如下所示:

new Ajax.Updater('targetDiv', 'data.php', { method: 'get', parameters: { myParam1: 'hello', myParam2: 'world'} });

Check out the AJAX section of the Prototype API. 查看原型API的AJAX部分。 In it, it talks about an option you can use called 'evalJS' that you can set to true. 在其中讨论了可以使用的名为“ evalJS”的选项,可以将其设置为true。 When you have this option set, any javascript returned by the updater will be evaluated and ran like normal. 设置此选项后,更新程序返回的所有javascript都将被评估并正常运行。

function ajaxUpdater(id, url) {
    new Ajax.Updater('targetDiv', 'data.php', {
      asynchronous: true,
      evalJS: true
    });
}

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

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