Aug 24

javascript浮点数四则运算存在bug,如:
0.4 + 2.3 = 2.6999999999999997
6.51 – 3.16 = 3.3499999999999996
26.1 * 3 = 78.30000000000001
26.4 / 3 = 8.799999999999999

下面代码可以得到精确值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//浮点数加法运算
function FloatAdd(arg1,arg2){
    var r1,r2,m;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2));
    return (arg1*m+arg2*m)/m;
}

//浮点数减法运算
function FloatSub(arg1,arg2){
    var r1,r2,m,n;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2));
    //动态控制精度长度
    n=(r1>=r2)?r1:r2;
    return ((arg1*m-arg2*m)/m).toFixed(n);
}

//浮点数乘法运算
function FloatMul(arg1,arg2)
{
    var m=0,s1=arg1.toString(),s2=arg2.toString();
    try{m+=s1.split(".")[1].length}catch(e){}
    try{m+=s2.split(".")[1].length}catch(e){}
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}


 //浮点数除法运算
function FloatDiv(arg1,arg2){
    var t1=0,t2=0,r1,r2;
    try{t1=arg1.toString().split(".")[1].length}catch(e){}
    try{t2=arg2.toString().split(".")[1].length}catch(e){}
    with(Math){
        r1=Number(arg1.toString().replace(".",""));
        r2=Number(arg2.toString().replace(".",""));
        return (r1/r2)*pow(10,t2-t1);
    }
}

代码出处:http://www.cnblogs.com/konooo/archive/2010/01/23/1654617.html

  • Share/Bookmark
Tagged with:
Aug 21

很久没学东西,没什么可写。
将前段时间无聊时写的一个js小游戏拿出来晒一晒。
模仿“是男人就下一百层”写的,只实现了最基本的功能,高手绕道。

js游戏-是男人就下一百层

js游戏-是男人就下一百层

毕竟不是自己的独立博客,没有FTP,上传文件也有诸多限制,压缩文件都无法上传,无奈~~

jquery.testGame.js
[code lang="javascript"]
(function($){
$.extend($.fn,{
testGame : function(options){
$.fn.testGame.defaults = {
level : 1
};

var opts = $.extend({}, $.fn.testGame.defaults, options);

var box = $(this);
var cloudCreateTimer;
var cloudCreateSpeed = 1000;
var dieHeight = box.height();
var scores = 0;

var h = new Man();

function run()
{
h.init();
//监听左右方向键
$(document).keydown(function(key){
if(key.keyCode == 37){
h.goLeft();
}else if(key.keyCode == 39){
h.goRight();
}
});
h.fallDown();
createCloud();
}

function Man()
{
this.body = $('

H

');
this.timer;

this.fallDownFrequency = 20;
this.fallDownSpeed = 3;
this.manMoveSpeed = 10;

this.stopTop;
this.stopLeft;
this.stopRight;
this.stopSpeed;

if (typeof Man._initialized == "undefined") {
//放置小人
Man.prototype.init = function() {
var box_width = box.width();
var box_height = box.height();
var man_init_w = box_width / 2;
this.body.appendTo(box);
this.body.css('left', man_init_w+'px');
this.body.css('top', '0px');
}

//向左走
Man.prototype.goLeft = function() {
var p = this.body.css('left');
p = p.substring(0, p.length - 2);
var q = parseInt(p) - this.manMoveSpeed;
q = q >= 0 ? q : 0;
this.body.css('left', q+'px');
this.leaveCloud();
}

//向右走
Man.prototype.goRight = function() {
var box_width = parseInt(box.width());
var p = this.body.css('left');
p = p.substring(0, p.length - 2);
var q = parseInt(p) + this.manMoveSpeed;
q = q = dieHeight){
clearInterval(manTimer);
gameOver();
}else{
var q = parseInt(p) + manFallDownSpeed;
manBody.css('top', q+'px');
}
}, this.fallDownFrequency);
this.timer = manTimer;
}
}

//被云朵接住
Man.prototype.stopFall = function(top, left, right, cloudSpeed){
var manTop = this.body.css('top');
var manLeft = this.body.css('left');
manTop = parseInt(manTop.substring(0, manTop.length - 2));
manLeft = parseInt(manLeft.substring(0, manLeft.length - 2));

//人被云朵接住
if1
{
this.stopTop = undefined;
this.stopLeft = undefined;
this.stopRight = undefined;
this.stopSpeed = undefined;
this.fallDown();
}
}

Man._initialized = true;
}
}

function Cloud()
{
this.body = $('

_______

');
this.frequency = 20;
this.speed = 3;
this.timer;

if (typeof Cloud._initialized == "undefined") {
//放置云朵
Cloud.prototype.init = function(){
var cloudLeft = Math.random() * 230;
this.body.appendTo(box);
this.body.css('top', dieHeight+'px');
this.body.css('left', cloudLeft+'px');
}

//云朵向上飘
Cloud.prototype.fly = function(){
var cloudBody = this.body;
var cloudTimer = this.timer;
var cloudSpeed = this.speed;
cloudTimer = setInterval(function(){
var p = cloudBody.css('top');
var cloudLeft = cloudBody.css('left');
p = parseInt(p.substring(0, p.length - 2));
cloudLeft = parseInt(cloudLeft.substring(0, cloudLeft.length - 2));
var cloudRight = cloudLeft + parseInt(cloudBody.width());

h.stopFall(p, cloudLeft, cloudRight, cloudSpeed);

if(p <= 0){
clearInterval(cloudTimer);
cloudBody.remove();
}else{
var q = parseInt(p) - cloudSpeed;
cloudBody.css('top', q+'px');
}
}, this.frequency);
}

Cloud._initialized = true;
}
}

//定时产生一片云
function createCloud()
{
cloudCreateTimer = setInterval(function(){
var c = new Cloud();
c.init();
c.fly();
scores++;
}, cloudCreateSpeed);
}

//游戏结束
function gameOver()
{
if(cloudCreateTimer != undefined)
{
clearInterval(cloudCreateTimer);
}
alert('游戏结束,您的得分:'+scores);
$('#man').remove();
$('.cloud').remove();
}

return this.each(function(){
run();
});
}
});
})(jQuery);

[/code]

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#contrainer{ position: absolute; width:300px; height: 450px; margin: 0px 0 0 2px; border: 2px #CCCCCC solid; overflow: hidden;}

#man{ position: absolute;}

.cloud{ position: absolute;}







<p class="msg"> 使用左右键控制,碰到上边缘或下边缘游戏结束</p>

<div id="contrainer">

</div>







$(document).ready(function(){

$("#contrainer").testGame();

})
  1. manTop >= top - (cloudSpeed + 2) && manTop = left && manLeft <= right)
    {
    this.stopTop = top;
    this.stopLeft = left;
    this.stopRight = right;
    this.stopSpeed = cloudSpeed;
    clearInterval(this.timer); //停止往下掉
    this.timer = undefined; //重置计时器
    this.body.css('top', top+'px');
    if(top = this.stopTop - (this.stopSpeed + 2) && manTop = this.stopLeft && manLeft <= this.stopRight []
  • Share/Bookmark
Tagged with:
Aug 19

IE浏览器存在一个BUG,当你使用label的for属性达到点击label使对应的表单元素聚焦,label中的内容为图片时,IE浏览器下不起作用。

例如:
[code lang="html"]


[/code]

我们希望得到的效果是:点击“中国银行”,复选框会被选上(或者取消)。在FireFox,CHROME等浏览器下是没有问题的,但是IE浏览器却不吃这套。

解决的办法是使用JS:
[code lang="javascript"]
window.onload = function(){
if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1)
{
var a = document.getElementsByTagName("label");
for(var i=0,j=a.length;i<j;i++){
if(a[i].hasChildNodes && a[i].childNodes.item(0).tagName == "IMG")
{
a[i].childNodes.item(0).forid = a[i].htmlFor;
a[i].childNodes.item(0).onclick = function(){
var e = document.getElementById(this.forid);
switch(e.type){
case "radio": e.checked|=1;break;
case "checkbox": e.checked=!e.checked;break;
case "text": case "password": case "textarea": e.focus(); break;
}
}
}
}
}
}
[/code]

原文地址:原文地址:http://snook.ca/archives/javascript/using_images_as

  • Share/Bookmark
Tagged with:
May 28

出处:http://hi.baidu.com/benben1006/blog/item/187deb77bc0e5319b151b974.html

数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。

在使用left jion时,on和where条件的区别如下:

1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。

2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。

假设有两张表:

表1:tab1

id
size
1
10
2
20
3
30

表2:tab2

size
name
10
AAA
20
BBB
20
CCC


两条SQL:
1、select * form tab1 left join tab2 on (tab1.size = tab2.size) where tab2.name=’AAA’
2、select * form tab1 left join tab2 on (tab1.size = tab2.size and tab2.name=’AAA’)

第一条SQL的过程:

1、中间表
on条件:
tab1.size = tab2.size
tab1.id tab1.size tab2.size tab2.name
1
10
10
AAA
2
20
20
BBB
2
20
20
CCC
3
30
(null)
(null)
|
|
2、再对中间表过滤
where 条件:
tab2.name=’AAA’
tab1.id tab1.size tab2.size tab2.name
1
10
10
AAA

第二条SQL的过程:

1、中间表
on条件:
tab1.size = tab2.size and tab2.name=’AAA’
(条件不为真也会返回 左表中的记录)
tab1.id tab1.size tab2.size tab2.name
1
10
10
AAA
2
20
(null)
(null)
3
30
(null)
(null)

其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和 right的特性的并集。 而inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。

  • Share/Bookmark
Tagged with:
May 21

http://t.qq.com/invite/0d3412e0841a6a34af33

http://t.qq.com/invite/607da97bb7e88afdc289

http://t.qq.com/invite/71fdab9f368eab61eb27

http://t.qq.com/invite/3005feba95e66db274c9

http://t.qq.com/invite/415a411605bf2c16ea41

http://t.qq.com/invite/35fb76c791f3f2b39281

http://t.qq.com/invite/7369bd70b0a2b179f405

  • Share/Bookmark
Tagged with:
preload preload preload
本WordPress博客由爱写字提供技术支持