簡體   English   中英

如何將此javascript代碼轉換為jquery

[英]How to convert this javascript code in to jquery

if (window.parent.frames['scripts1']) {
  if (window.parent.frames['scripts1'].document.documentElement) {
    var strSCRIPT = window.parent.frames['scripts1'].document.documentElement.textContent;
    if ((strSCRIPT.lastIndexOf('bbbbEND') - strSCRIPT.length) != -7) {
      window.parent.frames['scripts1'].document.location.href = 'test1.txt?refresh=' + Date();
    }
  } else {
    window.parent.frames['scripts1'].document.location.href = 'test1.txt?refresh=' + Date();
  }
}

我已經嘗試了很多東西,但沒有成功為跨瀏覽器編寫東西。

首先,重構代碼以消除重復:

var s = window.parent.frames['scripts1'];
if (s) {
    var d = s.document;
    var e = d.documentElement;
    var t = e ? e.textContent : null;
    if (!t || t.length - t.lastIndexOf('bbbbEND') != 7) {
        d.location.href = 'test1.txt?refresh=' + Date();
    }
}

有一個已確定的兼容性問題,但至少現在我們有機會發現它!

具體來說,IE8或更早版本不支持.textContent ,因此:

var s = window.parent.frames['scripts1'];
if (s) {
    var d = s.document;
    var e = d.documentElement;
    var t = e ? (e.textContent || e.innerText) : null;
    if (!t || t.length - t.lastIndexOf('bbbbEND') != 7) {
        d.location.href = 'test1.txt?refresh=' + Date();
    }
}

這里有一些jQuery概念材料可以幫助您入門:

// make sure to give your frame an ID, and then it's easy to access
var frame_a = $( "#frame_a" );        

// jQuery's content() function seems to only work on iFrames, not on Frames
var frame_a_document = $( frame_a[0].contentDocument );        

var frame_a_document_body = frame_a_document.find( "body" );
var frame_a_text = $( frame_a_document_body ).text();        

alert( frame_a_text );

請記住以下重要事實:在完成父文檔后加載框架,這意味着在加載框架之前將執行ready()函數。 如果你嘗試在ready()函數中訪問你的框架,很可能你什么也得不到,因為它尚未被加載 - 即它是競爭條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM