[英]jQuery .load() html page with javascript inside
i have a problem with loading.html pages (with JavaScript inside) into a div element.我在将.html 页面(内部有 JavaScript)加载到 div 元素时遇到问题。
This is my div:这是我的 div:
<div class="content"></div>
This is my php:这是我的 php:
<?php
if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
if(file_exists('pages/page_'.$page.'.html'))
echo file_get_contents('pages/page_'.$page.'.html');
else echo 'There is no such page!';
?>
This is my script:这是我的脚本:
var default_content="";
$(document).ready(function(){
checkURL();
$('ul li a').click(function (e){
checkURL(this.hash);
});
//filling in the default content
default_content = $('.content').html();
setInterval("checkURL()",250);
});
var lasturl="";
function checkURL(hash)
{
if(!hash) hash=window.location.hash;
if(hash != lasturl)
{
lasturl=hash;
// FIX - if we've used the history buttons to return to the homepage,
// fill the pageContent with the default_content
if(hash=="")
$('.content').html(default_content);
else
loadPage(hash);
}
}
function loadPage(url)
{
url=url.replace('#page','');
$('.content').css('visibility','visible');
$.ajax({
type: "POST",
url: "load_page.php",
data: 'page='+url,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0)
{
$('.content').html(msg);
}
}
});
}
When the page is loaded the JavaScript code inside the.html page isn't executed.加载页面时,不会执行 html 页面内的 JavaScript 代码。 If i click on the browser reload button the JavaScript code is executed.
如果我单击浏览器重新加载按钮,则会执行 JavaScript 代码。
How can i execute the JavaScript inside the pages using jquery?如何使用 jquery 在页面内执行 JavaScript?
your script does not make sense你的脚本没有意义
$.ajax({
type: "POST",
url: "load_page.php",
data: 'page='+url,
dataType: "html",
success: function(d){
$('.content').html(d);
}
});
is better and will execute javascrit page loaded from ajax更好,将执行从 ajax 加载的 javascrit 页面
Are the jQuery event handlers binded with live()
? jQuery 事件处理程序是否与
live()
绑定?
http://api.jquery.com/live/ http://api.jquery.com/live/
If javascript/jquery is attached to the dom after the page has loaded, normal click()
, change()
events wont be attached/executed.如果在页面加载后将 javascript/jquery 附加到 dom,则不会附加/执行正常的
click()
、 change()
事件。
You need to attach these events using live()
.您需要使用
live()
附加这些事件。
$('#some_element').live('click', function(){});
live()
will initialize the event handler whenever an element is attached to the page.每当元素附加到页面时,
live()
都会初始化事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.