`
Odysseus_110
  • 浏览: 117033 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

js notes

阅读更多

1 array and object definition
let's introduce this bit of terminology: defining an array with [] is called array literal notation and defining an object using the curly braces {} is called object literal notation.

2 <a href="#"><a> 的 # 都去掉,用js

 

$('a').css('cursor','pointer');

 
控制让它成为 手的形状。

3 关于 jquery 操作 checkbox:

加上.unbind("click") 这个来阻止 默认的 checkbox 的事件冒泡。要不然会点击checkbox的时候会弹出两次alert。

$('#my-div-id').unbind("click").click(function(){
    alert('only click once!');
  }

 

 

纯的checkbox的属性是 checked true/false

但是 jquery里面 不能写

$('#ckb_'+playerid).attr('checked','true');
$('#ckb_'+playerid).attr('checked','false');

 

而是要用:

jQuery 1.6+
Use the new .prop() function:
$(".myCheckbox").prop("checked", true);
$(".myCheckbox").prop("checked", false);


jQuery 1.5 and below
$('.myCheckbox').attr('checked','checked')
$('.myCheckbox').removeAttr('checked')

 

4. ajax 异步请求的时候需要注意的地方:

$.getJSON(action_fetch_players+pagenum+'&ut='+usertype,null,function call(data){
        var playerslist = data.playerlist;
	
        //append the checkbox all function
	$('#user_all').unbind("click").click(function(){
		
		var trArrays = [];
		var i = 0;
                //所以要实时的取得 页面的元素
		$('#column_table_hook tbody tr').each(function(){
			if(this.id != null&&this.id != ''&&this.id != 'tr_playersmgt_wait_hook') {
				trArrays[i] = this.id;
				i++;
			}
		});
                //这里不能用palyerlist 因为是异步请求 所以 playerslist  里面存的 东西会是之前的数据, 在click里 绑定的事件 得到的 playerslist  数据不同。
                /*if(playerslist!= null){
                  ......
                }*/
		if(trArrays != null){
			if(approve_checkbox_statue){				
				for(var i=0; i < trArrays.length; i++) {
					var playerid = trArrays[i].substring(5);
					$('#ckb_'+playerid).attr('checked','checked');
				}
				approve_checkbox_statue = false;
			}else{
				for(var i=0; i < trArrays.length; i++) {
					var playerid = trArrays[i].substring(5);
					$('#ckb_'+playerid).removeAttr('checked');
				}
				approve_checkbox_statue = true;
			}
		}
	});

});

 
5

var PAGE_ELEMENTS_WIDTH = {
	'#wrap' : 'width'
}
循环得到对象里的属性, 然后用 jquery得到 值的时候,
for(var key in PAGE_ELEMENTS_WIDTH){
   var attrname = PAGE_ELEMENTS_WIDTH[i];
   var wstr = $(key).css(attrname);
   var wstrlen = wstr.length;
}

在这里调试的时候 wstr 的typeof 值是 string, 但是无论是在wstr上面调用length还是调用indexof 都没有用, 老是报错, 说没有定义,最后只能用

wstr = String(wstr)

才能work。 

原因还不清楚,先记录下来,有可能是 jquery的问题,虽然返回了typeof 是string,但实际上类型可能不是string。

 

6 Password bug

 

由于浏览器的安全限制,除了IE外,password type其它浏览器会出现很多奇怪的现象。比如修改其它的字段会自动将password清空。
而且$('#password').val(password); 这个语句是不起作用的。
解决方法:
先判断是否是IE,如果不是IE,则先设置成text类型,设置完后再设置它的值。

if ( $.browser.msie ){
  $('#psd_td').html('<input type="password" name="password" id="password" class="regFormField" value="'+password+'"/>');
}else{
  $('#psd_td').html('<input type="text" name="password" id="password" class="regFormField" value=""/>');
  //this .type method doesn't work on IE
  document.getElementById('password').type='password';
  $('#password').val(password);
}

 
7

<input type="button" value="批量添加" onClick="addExperts();"/>

 

chrome 不停的报错:

Uncaught TypeError: object is not a function 
onclick

 
把函数名称改一下就好了,

可能是 chrome 内置了很多相同名称的函数,但具体也不清楚,难道连addExperts 这样的名字也占用了么?


8

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" >

 
调试ie 或者 360的时候,如果js无缘无故的出不来,每个页面都加上这个标头试试。


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics