我通过 jquery 调用 ajax 以加载 href。html 似乎可以正确加载。但是,加载的 html (href) 实际上不起作用。具体来说,我无法弄清楚为什么一旦链接通过 ajax 加载,当我单击它时它实际上并没有做任何事情。萤火虫没有错误,只是死链接。我的 .on() 方法不正确吗?
主页.html
<html>
<head></head>
<body>
<div id="connections">Placeholder Text</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var uri='home-ajax.html'; //first find out if this is created yet
var myVar=setInterval(function(){example_ajax_request()},8000); //to do that check for it ev. 8 sec.
function example_ajax_request() {
$.ajax({
type: 'GET',
url: 'home-ajax.html', //checking
dataType: 'html', //html is content inside home-ajax.html
success: function() {
$('#connections').load('home-ajax.html'); //if home-ajax exists replace connections div with its content
clearInterval(myVar); //stop the 8 sec. timer
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
$('myLink').on('click', function(e) { //using .on to actively bind new href link from ajax()
document.location.href="http://www.cnn.com";//when href with id='myLink' is clicked, go to www.cnn.com
});
</script>
</body>
</html>
主页-ajax.html
<p><a href="#" id="myLink">CNN</a></p>