javascript
  • jquery
  • css
  • bookmarklet
  • bookmarks
  • 2011-07-21 27 views 1 likes 
    1

    我正在創建一個簡單的小書籤(以前從未這樣做過),而腳本執行得很好(現在做得不多),嵌入的CSS樣式表不適用於HTML。CSS不適用於小書籤

    激活鏈接如下(域名變更):

    <a href="javascript:(function(){document.head.appendChild(document.createElement('script')).src='http://www.mydomain.co.uk/cloudlr/app/js/cloudlr.js?rand=' + Math.floor(Math.random()*99999);})();">Do this</a> 
    

    然後調用的腳本是:

    if (typeof jQuery == 'undefined') { 
    
        appendjQuery(); 
    
    } else { 
    
        jsVersion = $().jquery; 
        versionArray = jsVersion.split('.'); 
    
        if (versionArray[1] < 6) { 
    
         appendjQuery(); 
    
        } else { 
    
         runthis(); 
    
        } 
    } 
    
    function appendCSS() { 
    
        var cloudlrCSS = document.createElement('link'); 
        cloudlrCSS.type = 'text/css'; 
        cloudlrCSS.href = 'http://www.mydomain.co.uk/cloudlr/app/css/screen.css'; 
        cloudlrCSS.media = 'all'; 
        document.head.appendChild(cloudlrCSS); 
    
    } 
    
    function appendjQuery() { 
    
        var cloudlrJS = document.createElement('script'); 
        cloudlrJS.type = 'text/javascript'; 
        cloudlrJS.onload = runthis; 
        cloudlrJS.onreadystatechange = function() { 
    
         if (this.readyState == 'loaded' || this.readyState == 'complete') { 
          runthis(); 
         } 
    
        } 
        cloudlrJS.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'; 
        document.head.appendChild(cloudlrJS); 
    } 
    
    function runthis() { 
    
        appendCSS(); 
    
        $(document).ready(function() { 
    
         $("img").each(function() { 
          var $this = $(this); 
          var imageWidth = $(this).width(); 
           $(this).before(imageWidth); 
         }); 
    
         var cloudlrOverlay = '<div id="cloudlr-content">This is some content.</div><div id="cloudlr-overlay"></div>'; 
    
         $("body").append(cloudlrOverlay); 
    
        }); 
    
    
    } 
    

    希望這一切都非常簡單。

    腳本執行得很好(jQuery),但沒有應用CSS。如果我查看「生成的源代碼」,我可以看到CSS插入正常並且鏈接到該文件。所以我不確定發生了什麼事。

    我不熟悉這一點,並歡迎任何有關最佳實踐的其他建議以及解決方案。

    非常感謝, Michael。

    +0

    什麼瀏覽器這是什麼? –

    +1

    你可以嘗試設置'cloudlrCSS.rel ='stylesheet';' –

    +0

    Greg - 回答了!現在起作用了。誰曾想到?也許它是瀏覽器特定的。我正在使用Firefox 5.非常感謝。把它寫成一個問題,我會給你的觀點。保重。 –

    回答

    1
    $("img").each(function(i,n) { 
        var $this = $(n);//don't know why this is here but hey 
        var imageWidth = $(n).width(); 
         $(n).before(imageWidth); 
    }); 
    

    這是一個常見的錯誤......我認爲這可以幫助你

    +0

    感謝您的意見,我會考慮到這一點。有什麼機會可以幫助我解決樣式表問題? –

    +0

    '$(this).before(imageWidth);'你不能在它的寬度之前添加一個元素,然後在另一個之前添加一個元素...不是嗎? – Val

    相關問題