この一般原則を使用する必要があります
var x = {
version: 1,
alert1: function() {
alert("1hi1");
},
alert2: function() {
alert("1hi2");
}
};
var y = x;
var x = {
version: 2,
alert1: function() {
alert("2hi1");
},
alert2: function() {
alert("2hi2");
}
};
y.alert1();
x.alert1();
jsfiddleで
jquery はそのnoconflict
メソッドを提供し、多くのライブラリは同じ (必ずしも名前ではない) メソッドを提供します。ただし、ロードするスクリプトの複雑さに応じて、私の例を参照して自分で行うことができます。
2 つの異なるバージョンを注入し、上記の原則を使用してそれらを異なる変数に割り当てる方法を次に示します。
<div id="version1"></div>
<div id="version2"></div>
var script1 = document.createElement("script"),
script2 = document.createElement("script"),
oldD3;
function noConflict() {
oldD3 = d3;
console.log("loaded old");
script2.type = 'text/javascript';
script2.src = "http://d3js.org/d3.v3.min.js";
script2.addEventListener("load", ready, false);
document.head.appendChild(script2);
}
function ready() {
console.log("loaded new");
console.log(d3, oldD3);
document.getElementById("version1").textContent = oldD3.version;
document.getElementById("version2").textContent = d3.version;
}
script1.type = 'text/javascript';
script1.src = "http://d3js.org/d3.v2.min.js";
script1.addEventListener("load", noConflict, false);
document.head.appendChild(script1);
jsfiddleで