drudfe0446838 2015-06-16 10:29 采纳率: 0%
浏览 83

javascript函数冲突或css选择冲突

I have a team of people working on a webpage which has lots of javascript libraries included in it and lots of classes . It's quite possible that one or two of these libraries or our developer's codes have functions with similar names or with the css to have the same selectors and classes. like this :

Javascript :

<script src="/src1"> ....  function func1() {} ......</script>
              .
              .
              .
<script src="/src2"> ........ function func1() {} ...</script>

Or css :

.content {... background-color:#fff; ...}

and another .content{... background-color:red; ...}

I have actually found some solutions for css but I was wondering is there any way or javascript or php library that I could use to parse javascript files to change javascript functions and calls names to a unique name?

  • 写回答

2条回答 默认 最新

  • douyin2883 2015-06-16 10:42
    关注

    For css a wrapper class (on a higher dom level) or a prefixed-class-name (eg. .my-content and .your-content) would do the job.

    A wrapper class can be easily achived with sass or less. you do not need to change your existing code, you can just wrap it. But on the downside, you will not be able to use two .content classes at the time, since you need the wrapper class.

    For js there are many solutions. Depending on your project they are more or less usefull. You could wrap some of your js as a jQuery Plugins or npm modules, to name only a few.

    But I guess would need a simpler solution.

    Namespacing your js code is the easiest way.

    var yourFirstNamespace = {
    
        foo: func1() {
        },
    
        bar: func2() {
        }
    };
    
    var yourSecondNamespace = {
    
        foo: func1() {
        },
    
        bar: func2() {
        }
    };
    
    ...
    
    yourFirstNamespace.func1();
    yourSecondNamespace.func1();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算