2016-08-30 18 views
1

我正在嘗試使用我自己的css修改iframe中的某些div類,並更改Twitter Block | Drupal 8的樣子。但是,我遇到了很多錯誤,例如$ is not a function等....我認爲我的js加載速度太快(在我的jquery.js之前),但是,我檢查了元素,一切都應該工作。 (現在沒有錯誤)如何設計Drupal Twitter塊?

HTML

<iframe id="twitter-widget-0" class="twetter-timeline" > 
    <html> 
    <head></head> 
    <body> 
     <div class="timeline-Viewport"> 
      <ol class="timeline-TweetList"> 
      <li></li> 
      </ol> 
     </div> 
    </body> 
    </html> 
</iframe> 

注:我沒有對iframe這不是我自己的代碼控制。

JS

(function ($, Drupal, drupalSettings) { 
    Drupal.behaviors.yourbehavior = { 
    attach: function (context, settings) { 

     $('iframe').load(function() { 
    $('#twitter-wwidget-0').contents().find("body") 
    .append($("<style type='text/css'> .timeline-Widget{color:red;} </style>")); 
}); 


    } 
    }; 
})(jQuery, Drupal, drupalSettings); 

沒有錯誤,但它不工作...所以我不知道我做錯了。

更新: 我也試過這個代碼,但也沒有工作。

(function($) { 

$(document).ready(function() { 
    console.log('hello'); 
    $('#twitter-widget-0').css('width', '100px'); 
    }); 
}(jQuery)); 
+0

iframe的內容是否在您的域名或Twitter的? https://en.wikipedia.org/wiki/Same-origin_policy – ceejayoz

+0

嗨@ceejayoz它是在我的域名..感謝提問 –

+0

@ceejayoz任何想法= /? –

回答

1

CORS(跨來源資源共享)訪問控制防止在包含文檔從修改(或者甚至不知道)的iframe的內容,如果它的內容是從比含有文檔的不同結構域來源。有機制(如postMessage)繞過這個,但他們需要在iframe的內部和外部之間的合作。

在Twitter的情況下,他們有documentation關於如何調整Twitter帖子和Feed嵌入的樣式。 (它看起來像Twitter將樣式限制爲黑暗或光線主題,寬度,對齊方式和鏈接顏色。)

+1

另外,OP應該強制性地改變解決或覆蓋要求的方法。我建議使用twitter的[REST API](https://dev.twitter.com/rest/public)。 – SaidbakR

+0

@sємsєм我會看看上面的鏈接!但我真的很想知道爲什麼我不能在我自己的域名中擊中'