1、修改代码解决ie8下js报错的问题
错误信息:对象不支持此属性或方法 jquery-1.7.1.js 行4097 字符4
解决办法:通过debug工具定位到报错的代码 $this.siblings("li").removeClass("active");
将代码改为:$this.siblings().removeClass("active"); 即去掉了siblings()方法的参数,问题解决
2、在js代码中引用字符串类型的php对象时,要添加引号,避免js将其作为变量名处理
错误信息:Uncaught ReferenceError: high is not defined
错误代码:$pager['page_first'] = "javascript:gotoPage(1,$id,$type,$level)"; 当$level=high时报如上错误
解决办法:给$level添加引号:$pager['page_first'] = "javascript:gotoPage(1,$id,$type,'$level')";
3、html页面如果不设置编码的话,会出现乱码情况,解决办法是加入如下代码:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
4、jquery 删除
$('#id').remove();
5、js判断对象是否存在,jquery判断radio是否选中
if(document.getElementById('searchPlayer')){
if(!$("input[name='doublePlayer']:checked").val()){
alert('双打比赛需要两个人一起报名,请选择搭档');
$('#searchPlayer').focus();
return;
}
}
6、ie浏览器下,会因为关键字冲突,导致异常
<form method="post" action="<?php echo $this->Html->url('/questions/complete');?>" id="answerForm">
<input type='hidden' id='score' name='score' value='476'/>
<div class="d_c1"><a href="javascript:void(0)" onclick='submit()'>保存测评结果</a></div>
</form>
在ie下这时候点击保存按钮,会直接提交form,而不是执行submit function。
7、分享到新浪微博代码
function shareTSina(title,rLink,site,pic) {
title = "分享标题";
// pic = $(".p-img img").attr("src");
rLink = "<?php echo $this->Html->url('/questions/bdmtnSelfTesting', true);?>";
window.open("http://service.weibo.com/share/share.php?title=" +
encodeURIComponent(title.replace(/ /g, " ").replace(/<br \/>/g, " "))+ "&url=" + encodeURIComponent(rLink),
"分享至新浪微博",
"height=500,width=600,top=100,left=100,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no");
}
8、分享链接代码
1、引入 ZeroClipboard.js
<?php echo $this->Html->script(array('self-testing','zclip/ZeroClipboard'));?>
2、添加事件
<a class="copy" data-clipboard-text="<?php echo $this->Html->url('/questions/bdmtnSelfTesting', true);?>" href="javascript:void(0);"><img src="img/selfTest/lk3.jpg"></a>
$(function(){
// setup copy to clipboard
ZeroClipboard.setDefaults({moviePath: '<?php echo $this->Html->url('/', true)?>js/zclip/ZeroClipboard.swf'});
clip = new ZeroClipboard($('a.copy'));
clip.on('complete', function(client, args) {
Popup.showMessage("链接复制成功", "success", {
title: '系统提示',
ok: '返回'
});
});
})
9、验证手机号的正则表达式
if(!(/^1[3|4|5|7|8]\d{9}$/.test(mobile))){
alert("手机号码不正确,请输入正确的手机号");
return false;
}