js替换innerHTML只能在没有jQuery的情况下工作

时间:2012-10-31 12:49:05

标签: javascript jquery html

我原以为:

$('#slideshow_body').html(newContent);

等同于:

document.getElementById('slideshow_body').innerHTML=newContent;

但事实并非如此。只有两个作品中的后者=(我希望现代化并缩短我的代码。

newContent目前包含此字符串,但将包含任意数量的div:s:

      <script id="pauseTimesScript" type="text/javascript">
//<![CDATA[
  /* Define pauseTimes (array holding duration of each slide) */
  try {
                pauseTimes=null;
                pauseTimes=undefined;
                pauseTimes = new Array();       
                setPauseTimes();                
                /*alert('satte först till null, deklarerade sedan om.');*/
  } catch(e) {
                pauseTimes = new Array();       
                setPauseTimes();                
                /*alert('deklarerade utan att först sätta null.');*/
  }
  function setPauseTimes() {      
        pauseTimes[1]=10000;                    
  }
  //]]>
  </script>
  <div id="avbrott" style="display: none;">
    Tillf&Atilde;&curren;lligt avbrott, visar cachelagrat inneh&Atilde;&yen;ll sedan
    2012-10-31 14:04:52
  </div>
  <div id="canvas" style="width:1072px;height:732px;">
    <div id="slide1" class="slide" style="z-index: 1; display: block;">
      <div id="slide_bg" style=
      "float:left;height:100%;width:100%;background-color:#ffffff;background-image:url('http://bglabs.evade.netdna-cdn.com/45875kli90/71.jpg');">
      <div style=
      "background-color:#ff0000;color:#ffffff;float:none;text-align:center;margin-top:30px;padding:10px;font-weight:bold;font-size:58pt;"
        id="preview_title">
          Nu &Atilde;&curren;r det jul igen!
        </div>
        <div style="clear:both;float:none;"></div>
        <p style=
        "color:#0f00de;margin:10px 10px 0 20px;font-style:italic;font-size:42pt;" id=
        "preview_data_6">Tja, snart i alla fall =)</p>
      </div>
    </div>
  </div>

以下是我获取该字符串的方法,不知道它是否会改变任何内容......

function ajaxUpdate() {

    //Load current uri again asynchroneously
    var jqxhr = $.get(currentUri)
      .success(function(data) { newContent = data; })
      .error(function() { newContent = 'blank'; }); 

    //make sure we got the end token (</body>) so that our transmission wasn't interrupted in the middle of the request
    endtoken_test = newContent.search('</body>');

    //strip of everything before starttoken (<body>)
    newContent = newContent.substring(newContent.indexOf('<body id="slideshow_body">') +26 );

    //strip of everything before endtoken (</body>)
    newContent = newContent.slice(0, newContent.lastIndexOf("</body>"));

    if (endtoken_test != -1) {

1 个答案:

答案 0 :(得分:1)

他们应该以同样的方式行事。

有一个小提琴here

$(function(){
    $('#jq').click(function(){
        $('#content').html('jQuery');
    });

    $('#js').click(function(){
        document.getElementById('content').innerHTML = 'javascript';
    });
});

jQuery Docs

相关问题