个人备注 JS使用问题集锦

1、<class="xx" style="border-width:0px;"> 就是已经有class了,还要追加一些效果或者去除一些效果;


2、onclick回调函数

<script language="javascript">
 function createTr(){
  var tb=document.all.tb;
  var tr=tb.insertRow();
  var td=tr.insertCell();
  td.innerHTML="test";
  td.setAttribute("id","newTd");
  td.value="test";
  td.onclick=clickMe(this);
 }
 function clickMe(tobject){ var td=tobject.value; alert(td); }
 </script>
3、如果想该文件不缓存,实时刷新
<meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0">
 
4、JS资源跨域
对于浏览器来说,script标签的src属性所指向资源就跟img标签的src属性所指向的资源一样,都是一个静态资源,浏览器会在适当的时候自动去加 载这些资源,而不会出现所谓的跨域问题。这样我们就可以通过该属性将要访问的数据对象引用进当前页面而绕过js跨域问题。当然,前提是接口必须是返回一段js脚本,如一个json对象数组定义的脚本: 
modlist = [
{"modname": "mod1", "usernum": 200, "url": "/widget/info/1"},
{"modname": "mod2", "usernum": 300, "url" : "/widget/info/2"},

];
script标签也有一定的局限性,并不能解决所有js跨域问题。script标签的src属性值不能动态改变以满足在不同条件下获取不同数据的需求, 更重要的是,不能通过这种方式正确访问以xml内容方式组织的数据。 
 
5、location href 判断跳转页面
  var href = location.href;   if(href.indexOf('from=developer') > -1){ //从邮箱跳转过来的 
 
6、判断当前页面是哪个页面,给对应的页面标签加样式;
    var a = document.getElementById("menu").getElementsByTagName("a");


    if(location.href.indexOf("location") > -1 || 
       location.href.indexOf('search') > -1 || 
       location.href.indexOf('map') > -1 ){
        a[1].className = "current";
    }else if(location.href.indexOf('datamanager') > -1){
        a[3].className = "current";
    }else if(location.href.indexOf('apiconsole') > -1){
        a[4].className = "current";
    }else{
        a[0].className = "current";
    }
7、重定义IFRAME大小
T.dom.ready(function(){  /*高度的自适应,header部门目前的高度是 80px 底部状态栏高度为32px*/   function initHeight(){     var hei = T.page.getViewHeight();     document.getElementById('content').style.height = (hei - 80 - 32) + 'px'; //内容的高度;     //右侧部分:内容的高度减去两像素的border;     document.getElementById('right').style.height = (hei - 80 -32- 2) + 'px';      //左侧高度和右侧高度一致     document.getElementById('asideLayer').style.height = (hei - 100-32- 2) + 'px';      //搜索区高度 固定为 41px(40px的高度 + 1px的底边框);     //地图高度:     document.getElementById('container').style.height = (hei - 80 - 2 - 41 - 32) + 'px';        }   initHeight();     window.onresize = initHeight;   }) 

你可能感兴趣的:(个人备注 JS使用问题集锦)