JavaScript conflict with googleapi

JavaScript conflict with googleapi

本文关键字:googleapi with conflict JavaScript      更新时间:2023-09-26

我在html中有一个JavaScript。

它是:(用于我的网站滚动通知使用)。

<script>
JQ = $;
function JQ(element){
    if(arguments.length>1){
        for(var i=0,length=arguments.length,elements=[];i<length;i++){
    elements.push(JQ(arguments[i]));
    }
    return elements;
    }
    if(typeof element=="string"){
        return document.getElementById(element);
    }else{
        return element;
    }
}
var Class={
    create:function(){
        return function(){
            this.initialize.apply(this,arguments);
        }
    }
}
Function.prototype.bind=function(object){
    var method=this;
    return function(){
        method.apply(object,arguments);
    }
}
var Scroll=Class.create();
Scroll.prototype={
    initialize:function(element,height){
        this.element=JQ(element);
        this.element.innerHTML+=this.element.innerHTML;
        this.height=height;
        this.maxHeight=this.element.scrollHeight/2;
        this.counter=0;
        this.scroll();
        this.timer="";
        this.element.onmouseover=this.stop.bind(this);
        this.element.onmouseout=function(){this.timer=setTimeout(this.scroll.bind(this),1000);}.bind(this);
    },
    scroll:function(){
        if(this.element.scrollTop<this.maxHeight){
            this.element.scrollTop++;
            this.counter++;
        }else{
            this.element.scrollTop=0;
            this.counter=0;
        }
        if(this.counter<this.height){
            this.timer=setTimeout(this.scroll.bind(this),22);
        }else{
            this.counter=0;
            this.timer=setTimeout(this.scroll.bind(this),3126);
        }
    },
    stop:function(){
        clearTimeout(this.timer);
    }
}
var myscroll=new Scroll("myscroll",13);
</script>

之后,我也想为我的网站添加一个jquery函数,但当我从googleapi中包含jquery时,它会与我的其他js发生冲突。我已经搜索了解决方案,添加了类似jQuery.noConflict();的东西,但它仍然无法工作。下面是我添加到html后的代码,有JS错误。下拉菜单全部无法工作。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    var jq = jQuery.noConflict(); 
    jq(function(){
        jq("#paysubmitbtn").click(function(){
           if(jq("#paymount").val() >= $minco){
                jq("#error").text("({lang jncashout:text_07}"+parseInt(jq("#paymount").val()*$exco*$jsuse)+"$jifentitle {lang jncashout:text_08}"+jq("#paymount").val()*$exco*$chargeco1+"$jifentitle {lang jncashout:text_09})");
             }else{
             jq("#error").text("({lang jncashout:text_10}"+$minco+"$cashoutname)");
             return false;
           }
        });
    });
    jq(function(){
          jq("#paymount").blur(function(){
             if(jq("#paymount").val() >= $minco){
                    jq("#error").text("({lang jncashout:text_07}"+parseInt(jq("#paymount").val()*$exco*$jsuse)+"$jifentitle {lang jncashout:text_08}"+jq("#paymount").val()*$exco*$chargeco1+"$jifentitle {lang jncashout:text_09})");
                 }else{
                   jq("#error").text("({lang jncashout:text_10}"+$minco+"$cashoutname)");
                 }
          });
       });
</script>

我从谷歌铬控制台显示检查错误

Uncaught TypeError: ctrlObj.getAttribute is not a function common.js?kkZ:1243 
Uncaught TypeError: $(...).removeChild is not a function
Uncaught TypeError: ctrlObj.getAttribute is not a function

我在JS方面真的不太好,请帮我解决我需要改进的问题,谢谢。

我在您提供的代码段中看不到ctrlObj

但只是为了让您了解getAttribute()的javascript方法。

因此,如果ctrlObj是一个jquery对象,那么它可能无法很好地与getAttribute配合使用。所以在这种情况下,您可以使用jQuery .attr方法。

示例

var x=ctrlObj.attr("您的属性名称");