0

我曾想过:

$('#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) {
4

1 回答 1

1

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

在这里拉小提琴

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

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

jQuery 文档

于 2012-10-31T13:09:37.913 回答