如何仅在 div 加载时运行函数?
我只想在加载 div 时运行函数。
当我加载页面时,会加载许多文件。在列表的末尾,PHP 回显一个 div。当显示这个时,jQuery 应该运行一个函数。
我可以通过点击事件来完成此操作,但我希望它能够自动工作,而无需按下按钮。
这是它如何通过点击进行工作:
$("#PP_end_show").live("click",function(){
$("#PP_head_bar").slideToggle("slow");
$("#PP_info_file_wrap").slideUp("slow");
});
这是 PHP 回显的 div
:
<?php echo "<div id=\"PP_end_show\"></div>"; ?>
输出是在 AJAX 调用后生成的:
<form id="PP_search_input" method="post" name="search_ID" ONSubmit="xmlhttpPost('PP_search_stream_client.php', 'PP_search_input', 'PP_thumb_output', '<img src=\'images/wait.gif\'>');return false; ">
<input name="search_string" type="text" class="PP_input" id="search_string" value="<?php echo $_POST['search_string']; ?>"/>
<button type="submit" class="PP_submit" id="search_submit"> search </button>
在生成的输出的末尾,将打印特定的 div 并且应该触发新的 jQuery 函数。
I want to run a function only when a div gets loaded.
When I load the page, a number of files are loaded. At the end of the list, PHP echoes a div. When this one is displayed, jQuery should run a function.
I can do this with a click-event, but I want it to work automatically, without pushing a button.
This is how it works with a click:
$("#PP_end_show").live("click",function(){
$("#PP_head_bar").slideToggle("slow");
$("#PP_info_file_wrap").slideUp("slow");
});
This is the div
echoed by PHP:
<?php echo "<div id=\"PP_end_show\"></div>"; ?>
The output is generated after an AJAX call:
<form id="PP_search_input" method="post" name="search_ID" ONSubmit="xmlhttpPost('PP_search_stream_client.php', 'PP_search_input', 'PP_thumb_output', '<img src=\'images/wait.gif\'>');return false; ">
<input name="search_string" type="text" class="PP_input" id="search_string" value="<?php echo $_POST['search_string']; ?>"/>
<button type="submit" class="PP_submit" id="search_submit"> search </button>
at the end of the generated output the specific div will be printed and should trigger the new jQuery function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
这就是我解决这个问题的方法,假设我已经正确理解了它,其中提交表单 PP_search_input,返回所需的 html,然后应在其中执行 javascript 代码。
This is how I would tackle this issue, assuming I've understood it correctly in which the submission of the form PP_search_input, returns the html needed, in which then a the javascript code should be executed.
尝试将您的 javascript 代码放入生成的 ajax 代码中。
例如,如果你的 ajax 是从 php 代码生成的
,那么尝试像这样
Try putting your javascript code inside the generated ajax code.
For example if your ajax is generated from the php code
then try smth like this
您可以使用 livequery 插件
然后按如下方式使用它:
函数中的代码将执行当选择器中的元素 (
#PP_end_show
) 添加到 DOM 时You could use the livequery plugin
You would then use it as follows :
The code in the function would execute when the element in the selector (
#PP_end_show
) was added to the DOM为什么不将您的 javascript 与 DIV 代码一起回显呢?
您可以使用 PHP 来回显 Javascript,如下所示:
在代码之后:
写下:
这应该可以工作。
Why not echo your javascript along with the DIV code?
You can use PHP to echo Javascript like this:
After your code:
Write this:
This should work.
在 JS 中,将要执行的代码包装在函数中。即
在你的PHP中,写出PP_end_show div后调用该函数所需的JS。 IE
In your JS, wrap the code you want to execute in a function. i.e.
In your PHP, write out the JS needed to call that function after writing out the PP_end_show div. i.e.
这就是你所要求的:
然而,这不是一个好主意,因为它很愚蠢,而且你所要求的提示你不理解,但这就是你在这里的原因。由于您已经知道 AJAX 何时被调用,因此显式调用它并向其附加成功处理程序。
如果您没有像您所说的那样执行 AJAX,我会在输出的末尾放置一个脚本标记,并让它调用您的代码作为最简单的方法。即使如此,您也可以使用 jQuery.load 方法(而不是事件),该方法默认从您的响应中执行 JavaScript。
快乐编码。
This does what you are asking:
However this is not a good idea because its silly and what you are asking for hints you not understanding but that is why you are here. Since you already know when your AJAX is called, call it explicitly and attach a success handler to it.
If you were not doing AJAX as you say, I would put a script tag at the end of your output and have it call your code as the simplest approach. Even still, you could use the jQuery.load method (not event) which by default executes JavaScript from your response.
Happy coding.
保持 jquery live-click-function 不变
&在php中运行以下代码
keep that jquery live-click-function as it is
& run the below code in php
将实时事件绑定到由文档触发的自定义事件:
参考
Bind the live event to a custom event that is triggered by the document:
References
Jason Brumwell输入正确答案,也可以使用JQuery的
$.when
函数。http://api.jquery.com/jQuery.when/
Jason Brumwell enter right answer, also you can use the
$.when
function of JQuery.http://api.jquery.com/jQuery.when/
如果您想要在所需的 div 加载后执行的操作绑定到单击事件,只需在 ajax 请求末尾触发单击即可,例如:
这些人是对的,您正在向后执行这一切,伙计
If the action you want to perform once the desired div loads is bound to a click event, simply trigger click at the end of the ajax request something like:
These guys are right though, you are doing this all backwards mate
它必须是那个精确的 div 吗?在我看来,您应该在加载文档时执行该函数。这样您就知道所有元素都已加载。
Does it have to be that exact div? It seems to me that you should just execute the function on load of the document. That way you know all your elements are loaded.
试试这个,
Try this,
使用回调函数。
它是一个在处理事件后立即触发的函数。
所以对于 Jquery:
我认为这应该可以解决问题
Use a callback function.
Its a function that will be triggered as soon as your event is handled.
so for Jquery:
And I think that should do the trick