您好我正在使用链接workshop.rs/projects/coin-slider中提到的滑块代码。它可以独立工作但是当我使用onclick事件通过AJAX调用此页面然后将内容显示为div,这不起作用。只有图像显示没有任何影响..请建议!!!!!!
html文件:
<div id="nav">
<ul>
<li><a href="dbuelem.html">DBU Elimination</a></li>
<li><a href="contact.html">Contact us</a></li>
</ul>
</div>
<div id ="welcome">
</div>
JS代码:
window.onload = initAll;
function initAll() {
allLinks[i].onclick = mainProcess;
}
function mainProcess(evt){
if ((this.innerHTML)=='Contact us'){
document.getElementById("right1").innerHTML = "Contacts";
Contacts();
return false;
}
}
function Contacts(){
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
if (xhr) {
url = 'contactslide.html';
xhr.onreadystatechange = showContacts;
xhr.open("GET", url, true);
xhr.send(null);
}
else {
alert("Sorry, but I couldn't create an XMLHttpRequest");
}
return false;
}
function showContacts() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var outMsg = xhr.responseText;
}
else {
var outMsg = "There was a problem with the request " + xhr.status;
}
document.getElementById("welcome").innerHTML=outMsg;
}
}
contactslide.html
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript" src="coin-slider.min.js"></script>
<link rel="stylesheet" href="coin-slider-styles.css" type="text/css" />
</head>
<body>
<div id='coin-slider'>
<img src="images/1.jpg"/>
<span>Abhinav Singh</span>
<img src="images/2.jpg" />
<span>Pradeep sethi</span>
<img src="images/3.jpg" />
<span>Tracey Hare</span>
<img src="images/4.jpg" />
<span>Amarish Patel</span>
<img src="images/5.jpg" />
<span>Richa Misra</span>
<img src="images/6.jpg" />
<span>Vishal Anand</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#coin-slider').coinslider();
});
</script>
</body>
</html>
其中contactslide.html是链接中提到的文件...此文件独立工作正常,因为当我用AJAX调用它时它没有显示任何效果..请建议!!!!!!!! !!!!
答案 0 :(得分:1)
在进行ajax调用并设置slider
后,您需要在元素上再次初始化innerHTML
插件。
假设您使用的是jQuery,我们可以修改Contacts
和showContacts
方法,如下所示
function Contacts(){
$.ajax({
url: 'contactslide.html',
success: showContacts,
error: function(xhr, errorStatus){
$("#welcome").html("There was a problem with the request. " + errorStatus);
}
});
return false;
}
function showContacts(response) {
var html = $(response);
$("#welcome").html(html.find('body').html());
}