【问题标题】:how to isolate CSS in a website gadget? [duplicate]如何隔离网站小工具中的 CSS? [复制]
【发布时间】:2017-04-20 13:32:07
【问题描述】:

我有一个 livecore 网站,我正在尝试制作该网站的小工具以在其他网站上显示它的一部分,我面临的问题是如何将我的代码(JavaScript 和 CSS)与主机的代码隔离开来我的小工具的网站。 我使用这样的“匿名函数”:

(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.8.3') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else { // Other browsers
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) {
    /******* Load CSS *******/
    var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "http://localhost/aaa/widget/widget.css" 
        });
        css_link.appendTo('head');          
        var txt;
        txt='<div id="wrapper">';
        txt=txt+'<ul class="tabs">';
        txt=txt+'<li id="fixtures_tab"><a href="#fixtures">All</a></li>';
        txt=txt+'<li id="live_tab"><a href="#live">Live</a></li>';
        txt=txt+'<li id="finished_tab"><a href="#finished">finished</a></li>';
        txt=txt+'<li id="program_tab"><a href="#program">Program</a></li>';
        txt=txt+'<li id="postpond_tab"><a href="#postpond">Postpond</a></li>';
        txt=txt+'<li id="selected_tab"><a id="f" href="#fav">Selected (0)</a></li>';
        txt=txt+'</ul>';
        txt=txt+'<div class="tab-container">';
        txt=txt+'<div id="fixtures" class="tab-content"><script type="text/javascript">get_All_Today_Matches();</script></div>';
        txt=txt+'<div id="live" class="tab-content"><script type="text/javascript"></script></div>';
        txt=txt+'<div id="finished" class="tab-content"><script type="text/javascript"></script></div>';
        txt=txt+'<div id="program" class="tab-content"><script type="text/javascript"></script></div>';
        txt=txt+'<div id="postpond" class="tab-content"><script type="text/javascript"></script></div>';
        txt=txt+'<div id="fav" class="tab-content"><script type="text/javascript"></script></div>';
        txt=txt+'</div>';
        txt=txt+'</div>';
        $('#widget-container').html(txt);
    });
}

})(); // We call our anonymous function immediately 

JQuery 一切正常,但 CSS 不正常(CSS 文件已加载并且工作正常,但一些托管站点 CSS 影响了我的 CSS)我无法控制托管站点 CSS,我认为我有通过使用匿名函数很好地隔离了我的代码,但为什么我的 CSS 会受到网站原始 CSS 的影响?请帮帮我,我已经有一段时间了

【问题讨论】:

  • 您需要重置您认为他们可能设置的任何样式。例如,如果他们有div{background:#FF0000},那么你所有的 div 都会有红色背景。您可以通过设置透明或继承来恢复它。一个好的开始方式是.mycontainer *{font:inherit;(etc)}
  • 我尝试使用 CSS 重置但我担心它没有用它仍然是同样的问题。我也使用了!重要但没有回应。我的问题特别是字体大小和系列

标签: jquery css


【解决方案1】:

我知道两种可能的解决方案。

一种是通过iframe 将您的代码嵌入到另一个站点。您的 CSS 不会发生冲突,除非其他站点 specifically writes something 在 iframe 中修改您的 CSS。但默认情况下不会有问题。

否则,您必须将MyWidget_ 添加到所有类/ID 名称的开头,以防止与其他站点发生冲突。

【讨论】:

  • 我完全同意,这是唯一的方法,但也像 Arun 说的那样,我会删除任何 id 并选择类名以便能够使用多个小部件实例。
【解决方案2】:

我的解决方案是在顶部元素中添加一个 my-widget-wrapper 类,在这种情况下,我认为它是 id 为 wrapper 的元素。

然后将我的样式文件中的所有 css 规则指定为 my-widget-wrapper 类的子类。

例如:将 a 元素的颜色更改为绿色,而不是 css 规则

a {
    color: green;
}

我会用

.my-widget-wrapper a {
    color: green
}

旁注:当您开发小部件时,切勿在小部件元素中使用固定 ID,因为它会限制您在页面中只有一个小部件实例。不要使用id 属性来识别元素,而是使用自定义class 属性

【讨论】:

  • 我在 css 文件中使用类,包装器中的 id 仅用于 jquery。在 jquery 中,我添加了一些 html 并使用 widget.css 制作它们的样式,因此 id 包装器与 css 无关
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 2012-02-26
  • 1970-01-01
相关资源
最近更新 更多