JS冲突求解

wang888813 2011-04-22 04:06:17
代码1:
function $(id, tag){
var re = (id && typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagName(tag);
};
};
function addImg(url){
var img = new Image();
img.src = url;
return img;
};//创建缓存图片[图片地址]
function addEvent(el, s, fn){
if(!el){return}
el.attachEvent ? el.attachEvent('on' + s, fn) : el.addEventListener(s, fn, false);
}
function removeEvent(el, s, fn){
if(!el){return}
el.attachEvent ? el.detachEvent('on' + s, fn) : el.removeEventListener(s, fn, false);
}

Array.prototype.Add = function(i, v){
return this.splice(i, 0, v);
};//数组ADD
Array.prototype.Del = function(i){
return this.splice(i, 1);
};//数组DEL
//首页焦点滚动图
function FocusImg(){
var img = $("focus", "img");
var url = $("focus", "a");
var now = 1, len = img.length, tm;
var burl = $("focusImg", "a")[0];
var bul = $("focusImg", "ul")[0];
burl.appendChild(addImg(img[0].src));
burl.href = url[0].href;
var bimg = $("focusImg", "img")[0];
for (var i = 0; i < len; i++) {
bul.innerHTML += "<li>" + (i + 1) +"</li>";
}
var bli = $("focusImg", "li");
bli[0].className = "show";
function pfn(i){
bli[i].onmouseover = function(){
go(i);
}
bli[i].onclick=function(){
go(i);
window.open(url[i])
}
};
for (var i = 0; i < len; i++) {
pfn(i);
}
function xunhuan(){
if (/*@cc_on!@*/false) {
bimg.style.filter = "progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,motion=forward)";
bimg.filters[0].Apply();
bimg.filters[0].Play(duration = 1);
}
burl.href = url[now].href;
bimg.src = img[now].src;
for (var i = 0; i < len; i++) {
bli[i].className = "";
}
bli[now].className = "show";
(now < len - 1) ? now++ : now = 0;
}
function init(){
tm = setInterval(xunhuan, 5000);
}
function go(n){
clearInterval(tm);
now = n;
xunhuan();
init();
};
init();
}

代码2:

$(document).ready(function(){
jQuery.keyText(0);
$(".tao_search form").submit(function(){
if (('{|}'+keytext+'{|}').indexOf('{|}'+$('#s_key').val()+'{|}')>-1) {
$('#s_key').css('color', '#000');
$('#s_key').val('http://www.taobao.com/');
}
});
});
var keytimeid = null;
var keyflag = true;
var keytext = "你好";
var keyarr = keytext.split('{|}');
jQuery.keyText = function(n) {
if (keyflag) {
$('#s_key').val(keyarr[n]);
if (n+1>=keyarr.length) n = 0; else n++;
keytimeid = setTimeout("jQuery.keyText("+n+")", 4000);
}
}
function sType(str) {
var sA = $(".tao_choose").find('a');
sA.attr('class', '');
$("#s_type").val(str);
if (str=='item') {
sA.eq(0).attr('class', 'current1');
} else {
sA.eq(1).attr('class', 'current2');
}
$('#s_key').attr('class', 'tao_text1');
$('#s_key').focus();
return false;
}
function keyFocus() {
keyflag = false;
clearTimeout(keytimeid);
if (('{|}'+keytext+'{|}').indexOf('{|}'+$('#s_key').val()+'{|}')!=-1 || $('#s_key').val()=='') {
$('#s_key').val('');
$('#s_key').attr('class', 'tao_text1');
}
};

这两段代码有冲突,去掉第一个 第二个特效可以显示 ,去掉第二个,第一个特效能够显示。
去掉第一个的前一个function 第二个特效正常显示 第一个特效不显示 查了一下是$(美元符)冲突
对js不懂,希望各位高手能够指点 给出解决的方法,谢谢!
...全文
170 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2011-04-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 showbo 的回复:]

建议你还是改你自定义的$为其他的,虽然jq有noConflict来解决$冲突,但是你得用jQuery全名,麻烦
[/Quote]
jQuery(function($){

});
hch126163 2011-04-23
  • 打赏
  • 举报
回复
改应该函数名。不是很简单吗!有不是改应该很复杂的算法!

要不你就只用jquery的效果。 jquery 有很多插件。够你用了
Go 旅城通票 2011-04-22
  • 打赏
  • 举报
回复
建议你还是改你自定义的$为其他的,虽然jq有noConflict来解决$冲突,但是你得用jQuery全名,麻烦
zell419 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wang888813 的回复:]

引用 3 楼 zell419 的回复:
function $(id, tag){
var re = (id &amp;&amp; typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagNa……
[/Quote]
将第一段的$方法 。改个名字 。
第一段中所有用了$的 换成改过来的 。
你看下提示出了什么错误 。
这个应该还是不难的 。
ycproc 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hch126163 的回复:]

任意改一个不就可以了!

jquery 有防js 库冲突的!

http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729417.html
[/Quote]
下载了 倒入进去啊
wang888813 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zell419 的回复:]
function $(id, tag){
var re = (id && typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagName(tag);
};
……
[/Quote]

对JS一点不懂,给个明确方法,谢谢!
wang888813 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wang888813 的回复:]
引用 4 楼 hch126163 的回复:
当然改这段代码也行:
function $$(id, tag){
var re = (id &amp;&amp; typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getE……
[/Quote]
//menu
function menu(el, n, css){
addEvent(el, 'mouseover', function(){
el.className = el.className + ' '+ (el.className+'_');
$('m' + n).style.display = 'block';
})
addEvent(el, 'mouseout', function(){
el.className = el.className.split(' ')[0];
$('m' + n).style.display = 'none';
})
addEvent($('m' + n), 'mouseover', function(){
el.className = el.className + ' '+ (el.className+'_');
$('m' + n).style.display = 'block';
})
addEvent($('m' + n), 'mouseout', function(){
el.className = el.className.split(' ')[0];
$('m' + n).style.display = 'none';
})
}
function ttshow(close, open){
//debugger;
if(!$(close) || !$(open)){return}
addEvent($(close), 'click', function(){
var tar = $(close).parentNode.parentNode;
$(close).style.display = 'none';
$(open).style.display = 'inline-block';
while (tar) {
tar = tar.nextSibling;
if (tar.nodeType == 1) {
return tar.style.display = 'none'
}
}
})
addEvent($(open), 'click', function(){
var tar = $(open).parentNode.parentNode;
$(close).style.display = 'inline-block';
$(open).style.display = 'none';
while (tar) {
tar = tar.nextSibling;
if (tar.nodeType == 1) {
return tar.style.display = 'block'
}
}
})
}
//多级菜单
function mulitMenu(el){
if(!el) return;
var bchilds = [];
var p = el.parentNode.parentNode;
var b = el.parentNode;
while (b.nextSibling) {
b = b.nextSibling;
if (b.nodeType == 1) {
bchilds.push(b);
}
}
addEvent(el, 'click', function(){
if(bchilds.length >0){
reCss(p, 'show');
}

for (var i = 0, l = bchilds.length; i < l; i++) {
bchilds[i].style.display = bchilds[i].style.display == 'block' ? bchilds[i].style.display = 'none' : bchilds[i].style.display = 'block';
}
})
}




//nav


function navmenu(els,tars){
for(var i=0,l=els.length;i<l;i++){
showmenu(els[i],tars[i]);
}
function showmenu(el,tar){
addEvent($(el),'mouseover',function(){
reCss($(el),'on');
$(tar).style.display = 'block';
})
addEvent($(el),'mouseout',function(){
reCss($(el),'on');
$(tar).style.display = 'none';
})
addEvent($(tar),'mouseover',function(){
reCss($(el),'on');
$(tar).style.display = 'block';
})
addEvent($(tar),'mouseout',function(){
reCss($(el),'on');
$(tar).style.display = 'none';
})
}
}

wang888813 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hch126163 的回复:]
当然改这段代码也行:
function $$(id, tag){
var re = (id && typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagName(ta……
[/Quote]
$都要改成$$吗? 那只是部分代码 完整的是:


//滚动/切屏效果,[id,子容器/孙容器,方向,速度,上按钮,下按钮,分页切换时间,每次切屏的条数,显示当前页码]
function HtmlMove(id, tag, path, rate, upbt, downbt, pgtime, lis, show){
var c, now = 0, mous = false, ex, mx;

var fg = tag.split('/');
var o = $(id);
var os = $(o, fg[0])[0];//子容器
var as = $(o, fg[1]);//孙容器
var fx = (path == "scrollRight" || path == "scrollLeft") ? "scrollLeft" : "scrollTop";//方向
var ow = fx == "scrollTop" ? as[0].offsetHeight : as[0].offsetWidth;//单个块的宽
var pw = fx == "scrollTop" ? o.offsetHeight : o.offsetWidth;//整屏的宽
var pgli = lis;//每次切换条数
var pg = Math.floor((as.length + (pgli - 1)) / pgli);//总共多少页
var pgmx = ow * pgli;//每次翻动的像素
if (lis == Math.floor((pw + ow / 2) / ow)) {
var n = pg * lis - as.length;
for (var i = 0; i < n; i++) {
os.innerHTML += "<" + fg[1] + ">" + "</" + fg[1] + ">";
};
};//整屏切换时要补的空内容
os.innerHTML += os.innerHTML;
o.onmouseover = function(){
mous = true;
};
o.onmouseout = function(){
mous = false;
}
var d = setInterval(function(){
go((path == "scrollTop" || path == "scrollLeft") ? true : false);
}, pgtime);
if (upbt) {
var ut = $(upbt);
ut.onclick = function(){
clear(d);
go(true);
d = setInterval(function(){
go(true);
}, pgtime);
}
}
if (downbt) {
var dt = $(downbt);
$(downbt).onclick = function(){
clear(d);
go(false);
d = setInterval(function(){
go(false);
}, pgtime);
}
}
if (show) {
var so = $(show);
ut.s0 = ut.src;
ut.s1 = ut.src.replace(/(.*)(\.\w{3})/, "$1_$2");
dt.s0 = dt.src;
dt.s1 = dt.src.replace(/(.*)(\.\w{3})/, "$1_$2");
btnow();
}
function clear(){
clearInterval(d);
clearInterval(ex);
}
function btnow(){
so.innerHTML = (now + 1) + " of " + pg;
if (now < pg - 1 && now > 0) {
ut.src = ut.s0;
dt.src = dt.s0;
}
else
if (now == pg - 1) {
ut.src = ut.s1;
}
else
if (now == 0) {
dt.src = dt.s1;
}
}
function go(fxs){
if (mous) {
return;
};
if (fxs) {
if (show) {
if (now < pg - 1) {
now++;
btnow();
}
}
else {
if (now < pg) {
now++;
}
else {
now = 1;
o[fx] = 0;
};
}
}
else {
if (show) {
if (now > 0) {
now--;
btnow();
}
}
else {
if (now > 0) {
now--;
}
else {
now = pg - 1;
o[fx] = pg * pgmx;
};
}
}
mx = now * pgmx;
ex = setInterval(mov, 10);
function mov(){
var v = mx > o[fx] ? Math.ceil((mx - o[fx]) / rate) : Math.floor((mx - o[fx]) / rate);
o[fx] += v;
if (v == 0) {
clearInterval(ex);
ex = null;
mx = null;
};
}
}
}





//标签切换效果[标题框子元素("id/li"),内容框子元素("id/li"),事件(mouseover/click),默认显示第几条(-1表示在鼠标移出全部隐藏,仅在事件mouseover有效),轮播时间(1秒=1000)]
function SwitchTag(tit, box, s, show, time){
var t = tit.split('/'), b = box.split("/"), ts = $(t[0], t[1]), bs = $(b[0], b[1]), s = s || "onmouseover", now = show = show || 0, c;
for (var i = 0; i < ts.length; i++) {
ts[i].old = ts[i].className.replace("show", "");
bs[i].old = bs[i].className.replace("show", "");
reg(i);
}
function init(){
for (var i = 0; i < ts.length; i++) {
ts[i].className = ts[i].old;
bs[i].className = bs[i].old;
};
if (now != -1) {
ts[now].className += (t[2] || "") + " show";
bs[now].className += (b[2] || "") + " show";
}
}
function reg(i){
ts[i][s] = function(){
clearInterval(c);
now = i;
init();
}
if (show != -1 && time) {
bs[i].onmouseover = function(){
clearInterval(c);
};
bs[i].onmouseout = function(){
go();
};
ts[i].onmouseout = function(){
go();
}
}
if (show == -1 && s == "onmouseover") {
ts[i].onmouseout = function(){
now = -1;
init();
}
}
}
function go(){
c = setInterval(function(){
(now < ts.length - 1) ? now++ : now = 0;
init();
}, time);
}
if (show != -1 && time) {
go();
};
init();
}


//input
function checkInput(el){
if (!$(el)) {
return
}
var obj = $(el);
var old = obj.className;
var ov = obj.value;

addEvent(obj, 'click', function(){
obj.className = old + '_';
//if( obj.value == ov ){
obj.value = '';
//}

})
addEvent(obj, 'blur', function(){
ov = obj.value;
if (ov == '') {
obj.className = old;
obj.value = obj.defaultValue;
}
})
}

//ddlist select
function listchange(el){
if (!$(el)) {
return
}
var o = $(el);
var dt = $(el, 'dt')[0];
var dd = $(el, 'dd')[0];
var s = $(el, 'span');
addEvent(dt, 'mouseover', function(){
dd.style.display = 'block';
reCss(dt, 'show');
})
addEvent(dt, 'mouseout', function(){
dd.style.display = 'none';
reCss(dt, 'show');
})
addEvent(dd, 'mouseover', function(){
dd.style.display = 'block';
reCss(dt, 'show');
})
addEvent(dd, 'mouseout', function(){
dd.style.display = 'none';
reCss(dt, 'show');
})
for (var i = 0, l = s.length; i < l; i++) {
dbshow(s[i], dt)
}
}

function dbshow(el, target){
addEvent(el, 'mouseover', function(){
reCss(el, 'show')
});
addEvent(el, 'mouseout', function(){
reCss(el, 'show')
});
addEvent(el, 'click', function(){
target.innerHTML = el.innerHTML;
});
}


function reCss(el, css){
this.old = el.className;
el.className = el.className.indexOf(css) >= 0 ? el.className.replace(css, '') : (this.old + ' ' + css);
}

function ms(tt, box){
for (var i = 0, l = tt.length; i < l; i++) {
mss(tt, box, i, l)
}

}

function mss(el, tag, n, l){
addEvent(el[n], 'mouseover', function(){
for (var i = 0; i < l; i++) {
el[i].className = el[i].className.indexOf('_') >= 0 ? el[i].className.replace('_', '') : el[i].className;
tag[i].style.display = 'none';
}
el[n].className = el[n].className + '_';
tag[n].style.display = 'block';
})
}

//switchdiv
function switchtags(tt, box, n){
addEvent(tt[n], 'mouseover', function(){
for (var i = 0, l = tt.length; i < l; i++) {
tt[i].className = '';
box[i].style.display = 'none';
}
tt[n].className = 'show';
box[n].style.display = 'block';
})
}

hch126163 2011-04-22
  • 打赏
  • 举报
回复
当然改这段代码也行:
function $$(id, tag){
var re = (id && typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagName(tag);
};
};
function addImg(url){
var img = new Image();
img.src = url;
return img;
};//创建缓存图片[图片地址]
function addEvent(el, s, fn){
if(!el){return}
el.attachEvent ? el.attachEvent('on' + s, fn) : el.addEventListener(s, fn, false);
}
function removeEvent(el, s, fn){
if(!el){return}
el.attachEvent ? el.detachEvent('on' + s, fn) : el.removeEventListener(s, fn, false);
}

Array.prototype.Add = function(i, v){
return this.splice(i, 0, v);
};//数组ADD
Array.prototype.Del = function(i){
return this.splice(i, 1);
};//数组DEL
//首页焦点滚动图
function FocusImg(){
var img = $$("focus", "img");
var url = $$("focus", "a");
var now = 1, len = img.length, tm;
var burl = $$("focusImg", "a")[0];
var bul = $$("focusImg", "ul")[0];
burl.appendChild(addImg(img[0].src));
burl.href = url[0].href;
var bimg = $$("focusImg", "img")[0];
for (var i = 0; i < len; i++) {
bul.innerHTML += "<li>" + (i + 1) +"</li>";
}
var bli = $$("focusImg", "li");
bli[0].className = "show";
function pfn(i){
bli[i].onmouseover = function(){
go(i);
}
bli[i].onclick=function(){
go(i);
window.open(url[i])
}
};
for (var i = 0; i < len; i++) {
pfn(i);
}
function xunhuan(){
if (/*@cc_on!@*/false) {
bimg.style.filter = "progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,motion=forward)";
bimg.filters[0].Apply();
bimg.filters[0].Play(duration = 1);
}
burl.href = url[now].href;
bimg.src = img[now].src;
for (var i = 0; i < len; i++) {
bli[i].className = "";
}
bli[now].className = "show";
(now < len - 1) ? now++ : now = 0;
}
function init(){
tm = setInterval(xunhuan, 5000);
}
function go(n){
clearInterval(tm);
now = n;
xunhuan();
init();
};
init();
}
zell419 2011-04-22
  • 打赏
  • 举报
回复
function $(id, tag){
var re = (id && typeof id != "string") ? id : document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagName(tag);
};
};

应该主要就是这个方法重了 。
你换个方法名 。然后将第一段的都改下 。试试 。
hch126163 2011-04-22
  • 打赏
  • 举报
回复
任意改一个不就可以了!

jquery 有防js 库冲突的!

http://www.cnblogs.com/RascallySnake/archive/2010/05/07/1729417.html



乌镇程序员 2011-04-22
  • 打赏
  • 举报
回复
代码像一团乱麻,先帮你美化一下,以方便高手们帮你解决。。
function $(id, tag) {
var re = (id && typeof id != "string") ? id: document.getElementById(id);
if (!tag) {
return re;
}
else {
return re.getElementsByTagName(tag);
};
};
function addImg(url) {
var img = new Image();
img.src = url;
return img;
};
//创建缓存图片[图片地址]
function addEvent(el, s, fn) {
if (!el) {
return
}
el.attachEvent ? el.attachEvent('on' + s, fn) : el.addEventListener(s, fn, false);
}
function removeEvent(el, s, fn) {
if (!el) {
return
}
el.attachEvent ? el.detachEvent('on' + s, fn) : el.removeEventListener(s, fn, false);
}
Array.prototype.Add = function(i, v) {
return this.splice(i, 0, v);
};
//数组ADD
Array.prototype.Del = function(i) {
return this.splice(i, 1);
};
//数组DEL
//首页焦点滚动图
function FocusImg() {
var img = $("focus", "img");
var url = $("focus", "a");
var now = 1,
len = img.length,
tm;
var burl = $("focusImg", "a")[0];
var bul = $("focusImg", "ul")[0];
burl.appendChild(addImg(img[0].src));
burl.href = url[0].href;
var bimg = $("focusImg", "img")[0];
for (var i = 0; i < len; i++) {
bul.innerHTML += "<li>" + (i + 1) + "</li>";
}
var bli = $("focusImg", "li");
bli[0].className = "show";
function pfn(i) {
bli[i].onmouseover = function() {
go(i);
}
bli[i].onclick = function() {
go(i);
window.open(url[i])
}
};
for (var i = 0; i < len; i++) {
pfn(i);
}
function xunhuan() {
if (
/*@cc_on!@*/
false) {
bimg.style.filter = "progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,motion=forward)";
bimg.filters[0].Apply();
bimg.filters[0].Play(duration = 1);
}
burl.href = url[now].href;
bimg.src = img[now].src;
for (var i = 0; i < len; i++) {
bli[i].className = "";
}
bli[now].className = "show";
(now < len - 1) ? now++:now = 0;
}
function init() {
tm = setInterval(xunhuan, 5000);
}
function go(n) {
clearInterval(tm);
now = n;
xunhuan();
init();
};
init();
}

//代码2:
$(document).ready(function() {
jQuery.keyText(0);
$(".tao_search form").submit(function() {
if (('{|}' + keytext + '{|}').indexOf('{|}' + $('#s_key').val() + '{|}') > -1) {
$('#s_key').css('color', '#000');
$('#s_key').val('http://www.taobao.com/');
}
});
});
var keytimeid = null;
var keyflag = true;
var keytext = "你好";
var keyarr = keytext.split('{|}');
jQuery.keyText = function(n) {
if (keyflag) {
$('#s_key').val(keyarr[n]);
if (n + 1 >= keyarr.length) n = 0;
else n++;
keytimeid = setTimeout("jQuery.keyText(" + n + ")", 4000);
}
}
function sType(str) {
var sA = $(".tao_choose").find('a');
sA.attr('class', '');
$("#s_type").val(str);
if (str == 'item') {
sA.eq(0).attr('class', 'current1');
} else {
sA.eq(1).attr('class', 'current2');
}
$('#s_key').attr('class', 'tao_text1');
$('#s_key').focus();
return false;
}
function keyFocus() {
keyflag = false;
clearTimeout(keytimeid);
if (('{|}' + keytext + '{|}').indexOf('{|}' + $('#s_key').val() + '{|}') != -1 || $('#s_key').val() == '') {
$('#s_key').val('');
$('#s_key').attr('class', 'tao_text1');
}
};

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧