JavaScript笔记

文档

jQueryAPI:http://jquery.cuishifeng.cn/

jQuery插件库:http://www.jq22.com/

jQuery官网:https://jquery.com/

知识点

js代码某一行如果有错,其下面的代码无法执行;

<script type="javascript">,谨慎写!!!

IDEA的web项目:

src="loginform.js?..."像验证码加?以清除浏览器的js缓存

引入的.js共可置两个位置加载完页面有时需等一会js才有效(因为js还未下载完),放head里会解决这个问题但页面加载时间长,采用引入.js,这有利有弊

在html或jsp中的js代码可直接粘到引入的js文件中

.js放头部有时可能无效

页面资源文件(如css、js、图片等)只能放在webroot下面,放在WEB-INF下引用不了

引入.js、js写在jsp中各有利弊

有时js函数无效 -> 试着更改无效的函数的函数名

一个.jsp引入多个.js,注意引入顺序、有无冲突(引入的.jsp中也引入了.js,也有可能冲突)

引入js无效,写在jsp里以解决

一个.jsp中,只能有一个window.onload

window.onload的js写在jsp里

js能获取页面上所有元素;form表单提交后,request.getParameter只能获取表单里的元素

js写在jsp里时,还可以这样写:var cardID = "<%=cardID%>";  以方便获取值。

一小段code:

// 鼠标悬停时,滚动条缓慢滑动
var timer = null;
left.onmouseover=function () {
    // middle.scrollTop=0;//垂直滚动条
    timer=setInterval(function () {
        if(middle.scrollLeft==0){
            clearInterval(timer);
        }
        middle.scrollLeft=middle.scrollLeft-1;
    },10);
}
//鼠标离开,滚动条停止
left.onmouseout=function(){
    clearInterval(timer);
}
//鼠标点击,滚动条瞬间滚到顶部
left.onclick=function(){
    middle.scrollLeft=0;
    // middle.scrollLeft=920;//滚动条瞬间滚到底部
}
//获取一个组件的左绝对位置
function get_absolute_left(objectID) {
    var object=document.getElementById(objectID);
    var objectLeft=object.offsetLeft;
    var objectParent;
    while (object.offsetParent!=null){
        objectParent=object.offsetParent;
        objectLeft+=objectParent.offsetLeft;
        object=objectParent;
    }
    return objectLeft;
}
//获取一个组件的上绝对位置
function get_absolute_top(objectID) {
    var object=document.getElementById(objectID);
    var objectTop=object.offsetTop;
    var objectParent;
    while (object.offsetParent!=null){
        objectParent=object.offsetParent;
        objectTop+=objectParent.offsetTop;
        object=objectParent;
    }
    return objectTop;
}
//导航栏点击任何一个用例触发该函数
//arg0为触发该函数的html组件对象
//用法:<a href="faq.jsp" onclick="return UCClick(this)">
//点击会在iframe里显示faq.jsp
function UCClick(arg0) {
    var real_content=document.getElementById(iframe的id);
    real_content.src=arg0.href;
    return false;//超链接href和onclick同时存在,返回假href不执行
}
//浏览器后退一页
window.location.href = document.referrer;
window.history.back(-1);
//超链接href?传参,参数解析:
    try {
        var url=window.location.href;
        var result=url.split("?")[1];
        var keyValue=result.split("&");
        var obj={};
        for(var i=0;i<keyValue.length;i++){
            var item=keyValue[i].split("=");
            obj[item[0]]=item[1];
        }
        alert(obj);
    } catch (e) {
    }
//按钮?传参,....jsp?aValue=用户注册/管理员注册,解析
    var aValue="aValue";//要键aValue的值
    var str=window.location.search;
    if(str.indexOf(aValue)!=-1){
        var pos_start = str.indexOf(aValue) + aValue.length + 1;
        var pos_end=str.indexOf("&",pos_start);
        if(pos_end==-1){
            aValue=str.substring(pos_start);
            //aValue如果为百分号形式
            var aValue=decodeURI(aValue);
            if(aValue=="用户注册"){
                //do something
            }
            else if(aValue=="管理员注册"){
                //do something
            }
        }
    }

window.location:只能打开本网站的网址。window.open(....jsp):能打开任何网站的网址,这是2者主要区别。

一小段代码

/*  以下代码是为了防止重复提交登录表单,
    做法为开新窗体关当前窗体,还要在登录表单加上属性:target="_blank",该法已不使用。
    还是不开新窗体防止回退稳妥。  */
document.getElementById("form1").submit();
window.opener=null;
window.open('','_self');
window.close();
原文地址:https://www.cnblogs.com/yyjh/p/10969806.html