2014年11月25日 星期二

[Web] 使用Ajax更新局部頁面後javascript失效問題

  • 可以重新讀取js檔(因使用第三方js使用此方法較快)
<script type="text/javascript">
    function load_js()
    {
        var head= document.getElementsByTagName('head')[0];
        var script= document.createElement('script');
        script.type= 'text/javascript';
        script.src= '../thirdparty/Metro-UI-CSS-master/docs/js/metro.min.js';
        head.appendChild(script);
    }
   
    function js_AddToCar(Id) {
        $.ajax({
            type: "POST",
            url: "/AddCartFun",
            data: { msg: Id }

        })
        .done(function (data) {
            $('li#brid').html(data);
            load_js();
        });

    };
</script>
  • 若是自己的function可以使用重新綁定(live, bind)元件事件

2014年11月24日 星期一

[C/C++] C++ 運算子重載(4)

class E
{
public:
    int *num;   // 指標必須再new 1份
    E& operator=(const E& rhs)
    {
        this->num = new int((*rhs.num));   // 避免互相影響
        return *this;
    }
}
class A
{
    //member operator
    A& operator+=(const A& rhs)
    {
        this->num += rhs.num;
        return *this;
    }
    int num;
};
E test1;
*test1.num = 5;
    
E test2;
test2 = test1;

2014年11月23日 星期日

[C/C++] C++類別成員覆蓋性(3)

#include <iostream>
using namespace std;

class CA{
  public:
     CA(){a=2; b=3; c=4; }
     int a, b, c;
};

class CB: public CA{
  public:
     CB(){ a=100; b=200;}
     int a, b;      // 可以覆蓋覆類別
     int getX(){ return a*b; }
     int getY(){ return CA::a*CA::b*c; }    // 若要使用父類別
};

int main()
{
  CB b;
  cout<< "a*b= "<< b.getX()<< endl;
  cout<< "a*b= "<< b.getY()<< endl;
  b.a = 1;
  b.CA::a = 5;
  b.c = 2;
  cout<< "a*b= "<< b.getX()<< endl;
  cout<< "a*b= "<< b.getY()<< endl;
  return 0;
}

[C/C++] C++類別繼承筆記(2)

#include <iostream>

using namespace std;

class A
{
    public :
    A() : ia(10){}
    
    int ia;
};

class B : public A
{
    public :
    B() : ia(20){}      // error    ia不屬於本身類別成員
    B(){ ia = 20; }     // OK       須在建構式內設定值
    int ib;
};

int main()
{
    A ca;
    B cb;
    
    ca = cb;    // OK       A有的成員 B都會有
    cb = ca;    // error    B因繼承A 可能有A沒有的成員
    
   cout << ca.ia << endl;  // 20
   
   return 0;
}