我有這個功能,可以將HTML內容從頁面上的一個元素切換到另一個元素。我遇到的問題是該函數的第一次迭代運行時,它保留了原始的HTML值。我認爲下面的代碼會覆蓋當前的內容。首先我錯了?DOM上的innerHTML?
其次,我使用Ajax調用內容。這可能是一些DOM如何看不到變化的原因嗎?
這裏是我的代碼:
function subNavContent () {
//If ID is the reference.
//navContent = document.getElementByID('pageNav').innerHTML;
navContent = document.getElementsByClassName('pageNav')[0].innerHTML;
document.getElementById('subNav').innerHTML = navContent;
return;}
進行切換的HTML元素我打電話:
<nav id="subNav" class="aniSubNavOpen drop-shadow lifted">
</nav>
<div class="pageNav">
<h1 data-title="Welcome to The Mind Company">
<a>Welcome to The Mind Company</a></h1>
</div>
Ajax調用(和其他的東西爲了完整起見)
function subNavContent () {
navContent = document.getElementsByClassName('pageNav')[0].innerHTML;
document.getElementById('subNav').innerHTML = navContent;}
function subNavLoader () {
var subNav = document.getElementById('subNav');
var tmp = '';
if (subNav.className === 'aniSubNavClose') {
tmp = 'aniSubNavOpen'; }
else {
tmp = 'aniSubNavClose';}
subNav.className = tmp;
return;}
function sectionAssure(classID, type) {
subNavLoader ();
setTimeout(function () {
var tmp = '';
var sel = document.getElementsByTagName('section');
for (var i=0; i<sel.length; i++){
if (sel[i].id == classID) { tmp = 'block' } else { tmp = 'none' }
sel[i].style.display = tmp; }
subNavLoader (); }, (1500)); }
function loadContent (classID, url, type) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(classID).innerHTML=xmlhttp.responseText;}};
xmlhttp.open("GET", url, true);
xmlhttp.send();
subNavContent ();}
return; }
在您的示例中,您沒有pageNav類的元素,並且您有兩個ID爲subNav的元素。 – cababunga 2012-04-02 22:32:05