jquery ajax,如何在句柄页中调用 onload 事件
jquery ajax, how to call onload event in handle page?
我尝试通过jquery ajax将值从页面a发布到页面b。但是当我需要在句柄页面中加载事件时。 我尝试了两种方法,但都失败了。如何正确调用或这是一个错误?
答.php
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$("a").click(function(){
$.ajax({
url: "b.php",
dataType: "html",
type: 'POST',
data: "word="+"hello",
success: function(data){
$("#show").html(data);
}
});
});
});
</script>
<a href="#">click</a>
<div id="show"></div>
乙.php
<script language="JavaScript">
function callhello() {
alert('<?php echo $_POST['word']; ?>');
//many other code, for a test, I moved them. and replace a alert call.
}
</script>
<body onload="JavaScript:callhello()"><!-- way 1, put onload here, not run -->
Blah~~ Blah~~ Blah~~
<script language="JavaScript">// change to text/javascript or even remove, no effect
window.onload = function() {
callhello();
};
</script><!-- way 2, put onload here, still not run. -->
</body>
将 HTML 插入文档时,任何脚本都将与文档的上下文一起执行。 这意味着window.onload
是指当前window
的load
事件。 假设插入延迟(通过等待click
事件),则已触发window.onload
事件。 可以将函数附加到 load
事件,但它永远不会被触发。
最好将函数调用直接放入 script
元素中:
<script type="text/javascript"> // the language attribute is deprecated
callhello();
</script>
这意味着 jQuery 将在脚本添加到文档中时执行脚本。
您需要更改:
<script language="JavaScript">
自:
<script type="text/javascript">
您需要更改:
function callhello() {
alert('<?php echo $_POST['word']; ?>');
//many other code, for a test, I moved them. and replace a alert call.
}
自:
function callhello() {
alert('<?php echo $_POST["word"]; ?>');
//many other code, for a test, I moved them. and replace a alert call.
}
您需要更改:
<body onload="JavaScript:callhello()">
自:
<body onload="javascript://callhello();">
相关文章:
- 在phonegap中为android调用onload函数的最佳方式
- 如何调用表td标记内的Onload函数
- jsimage.onload调用父对象上的方法
- 当从Chrome扩展动态注入JS时,从onload()内部调用方法
- body onLoad未调用动态生成的javascript函数
- Window.onload 未被调用 [Ruby on Rails] [Vanilla Javascript]
- onError 在 onLoad 中加载图片失败时不调用
- XDomainRequest onload上对方法或属性访问的意外调用
- 在onload上调用两个函数不起作用
- 在 Body Onload 上调用三个 Javascript 函数
- Javascript - 加载前调用的“onload”函数
- 在代码隐藏中调用 window.onload 函数不起作用
- 在 SelectedIndexChanged 事件触发后调用 javascript window.onload
- 使用onload加载多个图像以调用函数,并且只有最后一个加载的图像调用该函数
- 为什么<车身负载=“;fn()">使用函数调用,但是window.onload=fn;使用处理程序函
- 对调用onLoad()的方法进行Javascript验证
- Silverlight不;t调用OnLoad参数
- 如何在AngularJS ng-include中获取调用onload的元素
- 方法仅调用 OnLoad Jquery
- jquery ajax,如何在句柄页中调用 onload 事件