如何用document.getElementById替换document.write(' ElementID')。innerHTML

时间:2016-03-29 08:21:54

标签: javascript php html getelementbyid document.write

只是为了让你知道我不是程序员。我尝试理解一些代码,并在可以使用反复试验的地方进行更改。 我有一个来自Joomla模块的以下代码,我需要更改函数:function pausescroller用于document.write的{​​{1}}函数,我不知道如何。我发布函数函数document.getElementById('ElementID').innerHTML的代码,我尝试实现上面的运气。前端没有显示任何内容:

pausescroller

这是整个JavaScript部分的代码:

function pausescroller(content, divId, divClass, delay){
  this.content=content //message array content
  this.tickerid=divId //ID of ticker div to display information
  this.delay=delay //Delay between msg change, in miliseconds.
  this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
  this.hiddendivpointer=1 //index of message array for hidden div
  document.getElementById('divId').innerHTML = "<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>";
  //document.write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>')
  var scrollerinstance=this
  if (window.addEventListener) //run onload in DOM2 browsers
    window.addEventListener("load", function(){scrollerinstance.initialize()}, false)
  else if (window.attachEvent) //run onload in IE5.5+
    window.attachEvent("onload", function(){scrollerinstance.initialize()})
  else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
    setTimeout(function(){scrollerinstance.initialize()}, 500)
}

2 个答案:

答案 0 :(得分:1)

更改此行:

document.getElementById('divId').innerHTML = "<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>";

到此:

document.getElementById(divId).innerHTML = '<div class="innerDiv" style="position: absolute; width: 100%" id="' + divId + '1">' + content[0] + '</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="' + divId + '2">' + content[1] + '</div>';

(第一个也是最后一个双引号应该是单引号,并且在调用divid时不要在var名称document.getElementById(divId).innerHTML周围加上引号)

-EDIT-但实际上你正在寻找的元素还没有,首先将父div放入html '<div id="myDiv" class="divClass" style="position: relative; overflow: hidden"></div>,然后var divId = 'myDiv';调用函数:

pausescroller(content, divId, divClass, delay);

答案 1 :(得分:0)

您设置的值innerHTML需要是一个完整的字符串 - 向每一方添加'将修复它:

document.getElementById('divId').innerHTML = '"<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>";'