V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
older
V2EX  ›  问与答

这两段jquery 代码是不是有命名冲突

  •  
  •   older · 2013-12-10 10:16:47 +08:00 · 2632 次点击
    这是一个创建于 3817 天前的主题,其中的信息可能已经有所发展或是发生改变。
    两段代码放一起,另一个就无效了。
    <script type="text/javascript">
    $(".list li a").click(function(){
    $(".list li a").removeClass("sel");
    $(this).addClass("sel");
    $(".list li a").css({background:"none"})
    $(".sel").css({background:"#383838"})
    })
    $(".list li a").active(function(){
    $(".list li a").not(".sel").css({background:"none"})
    $(this).css({background:"#383838"})
    })
    </script>

    <script type="text/javascript">
    //获取id
    function $ (id)
    {
    return typeof id === "string" ? document.getElementById(id) : id;
    }
    //获取tagName
    function $$ (elem, oParent)
    {
    return (oParent || document).getElementsByTagName(elem);
    }
    //获取class
    function $$$ (className, oParent)
    {
    var aClass = [];
    var reClass = new RegExp("(//s|^)" + className + "($|//s)");
    var aElem = $$("*", oParent);
    for (var i = 0; i < aElem.length; i++) reClass.test(aElem[i].className) && aClass.push(aElem[i]);
    return aClass
    }
    //初始化对象
    function Roll ()
    {
    this.initialize.apply(this, arguments)
    }
    Roll.prototype =
    {
    initialize: function (obj)
    {
    var _this = this;
    this.obj = $(obj);
    this.oUp = $$$("up", this.obj)[0];
    this.oDown = $$$("down", this.obj)[0];
    this.oList = $$$("list", this.obj)[0];
    this.aItem = this.oList.children;
    this.timer = null;
    this.iNow = 0;
    this.iHeight = this.aItem[0].offsetHeight;
    this.oUp.onclick = function ()
    {
    _this.up()
    };
    this.oDown.onclick = function ()
    {
    _this.down()
    }
    },
    up: function ()
    {
    this.oList.insertBefore(this.aItem[this.aItem.length - 1], this.oList.firstChild);
    this.oList.style.top = -this.iHeight + "px";
    this.doMove(0)
    },
    down: function ()
    {
    this.doMove(-this.iHeight, function ()
    {
    this.oList.appendChild(this.aItem[0]);
    this.oList.style.top = 0;
    })
    },
    doMove: function (iTarget, callBack)
    {
    var _this = this;
    clearInterval(this.timer)
    this.timer = setInterval(function ()
    {
    var iSpeed = (iTarget - _this.oList.offsetTop) / 5;
    iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    _this.oList.offsetTop == iTarget ? (clearInterval(_this.timer), callBack && callBack.apply(_this)) : _this.oList.style.top = iSpeed + _this.oList.offsetTop + "px"
    }, 30)
    }
    };
    window.onload = function ()
    {
    new Roll("side");
    };
    </script>
    7 条回复    1970-01-01 08:00:00 +08:00
    lingyired
        1
    lingyired  
       2013-12-10 10:18:54 +08:00   ❤️ 1
    function $ (id)
    {
    return typeof id === "string" ? document.getElementById(id) : id;
    }

    这里,就把jquery 的$ 给覆盖掉了。让我想起蛋疼的discuz。
    lingyired
        2
    lingyired  
       2013-12-10 10:21:20 +08:00   ❤️ 1
    解决方法是:
    把function $ (id) 中的$ 替换城getById,包括下面的引用。
    或者,使用 jQuery.noConflict() 让jQuery 释放 $ 的使用权,然后使用jQuery 代替
    airyland
        3
    airyland  
       2013-12-10 12:52:34 +08:00
    更加规范的是所有用到jQuery的代码都这样写

    (function($){
    //
    })(jQuery);
    jemygraw
        4
    jemygraw  
       2013-12-10 12:55:41 +08:00
    @airyland 说的对。或者简单一点放在$(document).ready(function(){...});里面。
    zealinux
        5
    zealinux  
       2013-12-10 15:25:41 +08:00
    在问问题的时候就不能把代码给弄整齐些吗???
    ianva
        6
    ianva  
       2013-12-10 15:30:01 +08:00
    帖代码用github
    <script src="https://gist.github.com/anonymous/7886958.js"></script>
    alsotang
        7
    alsotang  
       2013-12-10 17:38:31 +08:00
    明显是

    function $(id)

    这一句把 JQ 废了嘛。
    放进闭包里吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1228 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:22 · PVG 02:22 · LAX 11:22 · JFK 14:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.