顯示具有 JavaScript 標籤的文章。 顯示所有文章
顯示具有 JavaScript 標籤的文章。 顯示所有文章

2015年9月24日 星期四

AngularJS Breadcrumb With Route

同步發布於 http://villebez.logdown.com/posts/2015/07/20/angularjs-breadcrumb
AngularJS v1.4.4
Project結構:
-js/app.js
-js/route.js
-js/breadcrumb/directive.js
-js/breadcrumb/factory.js
-js/vendors/angular-route.min.js
-js/vendors/angular.min.js
-template/breadcrumb.html
-template/hello.html
-template/world.html
-template/index.html
-index.html
第一次寫AngularJS文章就挑戰Breadcrumb,寫得非常簡單陽春,
在寫這篇找資料時,意外找到一個更好的做法,提供參考
Auto-breadcrumbs with angular-ui-router
不過我的作法是手動breadcrumbs 冏rz...
那就來獻醜了...
首先呢~當然是看index.html
引入所需要的JS,body就只放breadcrumb directive以及view。

 data-ng-app="bcapp">

 charset="utf-8">
</span>BreadCrumb Demo<span class="nt" style="margin: 0px; padding: 0px; color: rgb(38, 139, 210) !important; font-weight: bold !important;">






-directive></breadcrumb-directive>
data-ng-view>
接著看看app.js、route.js
app 就只有迴圈設定$routeProvider,將route config抽出來至route.js
(function() {
    'use strict';
    angular.module('bcapp', [ 'ngRoute', 'components.breadcrumb' ]).config(configure);
    configure.$inject = [ '$routeProvider' ];
    function configure($routeProvider) {

        // Url 設定
     for ( var locationPath in appConfig.locationConfig) {
            var urlPattern = locationPath;
            var config = appConfig.locationConfig[locationPath];
            $routeProvider.when(urlPattern, config);
        }
    }
})();
route config簡單的設定urlPattern、template與resolve對該頁設定breadcrumb
window.appConfig = window.appConfig || {};
window.appConfig.locationConfig = {
    '/': {
        templateUrl: '/template/index.html',
        resolve : [["Breadcrumbs",function(Breadcrumbs){
            Breadcrumbs.init();
            
        }]]
    },
    '/hello': {
        templateUrl: '/template/hello.html',
        resolve : [["Breadcrumbs",function(Breadcrumbs){
            Breadcrumbs.init();
            Breadcrumbs.push({
                 "name": "Hello",
            });
        }]]
    },
    '/world': {
        templateUrl: '/template/world.html',
        resolve : [["Breadcrumbs",function(Breadcrumbs){
            Breadcrumbs.init();
            Breadcrumbs.push({
                 "name": "World",
            });
        }]]
    }
};
再來看看breadcrumb directive.js, factory.js
directive很基本,指定restrict,載入templateUrl,注入Breadcrumbs factory並set scope.breadcrumbs讓html可以使用這個factory
(function(){
    'use strict';
    angular.module('components.breadcrumb',[]).directive('breadcrumbDirective', Directive);
    
    Directive.$inject = 
        ['Breadcrumbs'];
    
    function Directive(Breadcrumbs) {
        var directive = {
            scope: {
                
            },
            restrict:    'E',
            templateUrl : '/template/breadcrumb.html',
            link : link  
        };
        
        return directive;
        
        function link(scope, elem, attrs, ctrl) {
            scope.breadcrumbs = Breadcrumbs;
        }
    }
})();
factory提供了幾個function
init初始化
set傳入Array設定整個breadcrumb
push傳入一筆資料進入Array
getAll取得整個Array
getFirst拿到第一筆資料
(function(){
    'use strict';
    angular
        .module('components.breadcrumb')
        .factory('Breadcrumbs', factory);

    factory.$inject = [];

    function factory() {
        var self = this;
        self.breadcrumbs  = [];

        // Interface
     self.init = init;
        self.set  = set;
        self.push = push;
        self.getAll = getAll;
        self.getFirst = getFirst;

        // Implement
     function init(){
            self.breadcrumbs =[{
                 "name": "首頁",
                 "path": "#/"
            }];
        }
        
        function set (urls) {
            self.breadcrumbs = urls;
        }
        
        function push (url){
            self.breadcrumbs.push(url);
        }
        
        function getAll(){
            return self.breadcrumbs;
        }
        
        function getFirst(){
            return self.breadcrumbs[0] || {};
        }
        
        return self;
    }
})();
最後就是四個template,其實沒什麼東西,只是為了呈現不同頁面與文字
-template/breadcrumb.html
class="breadCrumb">
  • ng-repeat="breadcrumb in breadcrumbs.getAll()" class="node"> -switch on="$last"> ng-switch-when="true">{{breadcrumb.name }} ng-switch-default> href="{{ breadcrumb.path }}" class="hand">{{ breadcrumb.name }} </ng-switch> ng-if="!$last" class="divider">>
  • -template/hello.html
    Hello
    
    -template/world.html
    world
    
    -template/index.html

    2010年3月30日 星期二

    <SCRIPT LANGUAGE="JavaScript">
    var myDate = new Date();
    myDate.getYear(); //獲取當前年份(2位)
    myDate.getFullYear(); //獲取完整的年份(4位,1970-????)
    myDate.getMonth(); //獲取當前月份(0-11,0代表1月)
    myDate.getDate(); //獲取當前日(1-31)
    myDate.getDay(); //獲取當前星期X(0-6,0代表星期天)
    myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
    myDate.getHours(); //獲取當前小時數(0-23)
    myDate.getMinutes(); //獲取當前分鐘數(0-59)
    myDate.getSeconds(); //獲取當前秒數(0-59)
    myDate.getMilliseconds(); //獲取當前毫秒數(0-999)
    myDate.toLocaleDateString(); //獲取當前日期
    var mytime=myDate.toLocaleTimeString(); //獲取當前時間
    myDate.toLocaleString( ); //獲取日期與時間

    if (mytime<"23:30:00")
    {
    alert(mytime);
    }
    </SCRIPT>

    2.

    <%
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    java.util.Date currentTime = new java.util.Date();//得到當前系統時間

    String str_date1 = formatter.format(currentTime); //將日期時間格式化
    String str_date2 = currentTime.toString(); //將Date型日期時間轉換成字符串形式
    %>

    2009年9月1日 星期二

    CSS/javascript 在 IE 與 Firefox 下的差別

    鑑於寫網頁時,經常碰到 IEFirefox 瀏覽器相容性的問題,即同樣的 CSS 或 javascript code,在兩個瀏覽器上看起來有所偏差,造成設計上的困擾,因此我從網路上整理了下面這些常見的 IE 與 Firefox 解析 CSS/javascript 上的不同之處以及解決方法。

    1.
    Firefox 不支援 innerText。Firefox 支援 innerHTML 但並不支援 innerText,不過可以使用 textContent 來達到 innerText 的效果。若不想用 textContent,如果內容字串不包含 HTML,可以 innerHTML 代替。

    2.
    關於禁止選取網頁內容。
    在 IE 中,一般使用用 javascript:obj.onselectstart=function(){return false;}
    而 Firefox 則必須用 CSS:-moz-user-select:none

    3.
    對 Firefox 而言,將 div 的 margin-left, margin-right 設為 auto 時,是自動置中的,但 IE 則不是。

    4.
    濾鏡的支援(如透明濾鏡)
    IE:filter: alpha(opacity=10);
    Firefox:-moz-opacity:.10;

    5.
    事件偵測。
    IE:obj.setCapture() 、obj.releaseCapture()
    Firefox:
    document.addEventListener("mousemove",mousemovefunction,true);
    document.removeEventListener("mousemove",mousemovefunction,true);

    6.
    div 等元素的邊界。
    例如:指定一圖層的 CSS 為:{width:100px; height:100px; border:#000000 1px solid;} IE:div 的寬度(包括邊框寬度)=100px,div的高度(包括邊框寬度)=100px;Firefox:div的寬度(包括邊框寬度)=102px,div的高度(包括邊框寬度)=102px;但若加上 XHTML 標準後,兩個瀏覽器就都為 102px 了!

    7.
    ul 在 Firefox 和 IE 下的不同呈現。
    ul 在 Firefox 下有 padding 值,卻沒有 margin 值;而在 IE 下正好相反,ul 有個 margin 值,卻沒有 padding 值。在 Firefox 下,ul 的 list-style 默認是處於內容的外邊緣的。但可以利用 CSS 可將 list-style 置為內容的內邊緣。
    適合兩個瀏覽器的 ul 設定為:padding:0; margin:0; list-style:inside; 或者 padding:0; margin:0; list-style:none。
    詳細圖文說明請見:wowbox blog (網頁設計知識庫)

    8.
    IE 和 Firefox 對 CSS margin 與 padding 的解釋不一樣。最簡單的方法是利用 CSS hacks,先 reset 各 selector 的 margin, padding。
    * {margin:0; padding:0;}
    關於 CSS hacks,原始來源翻譯轉載Yahoo YUI: Reset CSS

    9.
    FireFox不支援捲軸變色。

    10.
    在元素上設定背景色時,IE 是作用在 padding + content ,而 Firefox 則是作用在 border + padding + content 上。

    若要使網頁在 IE 與 Firefox 下都能夠順利呈現,有兩個常用的方法:

    1.
    判斷瀏覽器類型。
    var isIE=document.all?true:false;
    若支援 document.all,變數 isIE=true,否則 isIE=false

    2.
    在不同瀏覽器下的 CSS 處理。
    通常用 !important 來自動優先使用 CSS 語法(僅 Firefox 支援)
    如:{border-width:0px!important;border-width:1px;}
    在 Firefox 下這個元素是沒有邊框的,在 IE 下邊框寬度是 1px

    對經常利用 CSS 來美化排版網頁的使用者來說,有一個輔助編輯的工具是必須的,在 IE 上有 Internet Explorer Developer Toolbar,而 Firefox 上有 Web Developer 這些 Plugin,但如果能一次支援這兩種瀏覽器的呈現,那就更棒了!因此最後,推薦一個能夠同時支援 IE 與 Firefox 的 CSS 編輯軟體:CSSVista。它可以讓設計者同時、即時預覽這兩個瀏覽器下的結果,此外,也有自動 CSS 語法的提示喔!

    轉載自 http://oddlee.blogspot.com/2007/09/cssjavascript-ie-firefox.html

    JavaScript Document 屬性

    JavaScript - Document對象內容集合


    document.title //設置文檔標題等價於HTML的title標籤

    document.bgColor //設置頁面背景色

    document.fgColor //設置前景色(文本顏色)

    document.linkColor //未點擊過的鏈接顏色

    document.alinkColor //激活鏈接(焦點在此鏈接上)的顏色

    document.vlinkColor //已點擊過的鏈接顏色

    document.URL //設置URL屬性從而在同一窗口打開另一網頁

    document.fileCreatedDate //文件建立日期,只讀屬性

    document.fileModifiedDate //文件修改日期,只讀屬性

    document.fileSize //文件大小,只讀屬性

    document.cookie //設置和讀出

    cookiedocument.charset //設置字符集 簡體中文:gb2312

    ———————————————————————

    常用對象方法

    document.write() //動態向頁面寫入內容

    document.createElement(Tag) //創建一個html標籤對象

    document.getElementById(ID) //獲得指定ID值的對象

    document.getElementsByName(Name) //獲得指定Name值的對象

    document.body.appendChild(oTag)

    ———————————————————————


    body-主體子對象

    document.body //指定文檔主體的開始和結束等價於body.../body

    document.body.bgColor //設置或獲取對象後面的背景顏色

    document.body.link //未點擊過的鏈接顏色

    document.body.alink //激活鏈接(焦點在此鏈接上)的顏色

    document.body.vlink //已點擊過的鏈接顏色

    document.body.text //文本色

    document.body.innerText //設置body>…/body>之間的文本

    document.body.innerHTML //設置body>…/body>之間的HTML代碼document.body.topMargin //頁面上邊距

    document.body.leftMargin //頁面左邊距

    document.body.rightMargin //頁面右邊距

    document.body.bottomMargin //頁面下邊距

    document.body.background //背景圖片


    document.body.appendChild(oTag) //動態生成一個HTML對象


    常用對象事件

    document.body.onclick="func()" //滑鼠游標單擊對象時觸發

    document.body.onmouseover="func()" //滑鼠游標移到對象時觸發

    document.body.onmouseout="func()" //滑鼠游標移出對象時觸發

    ———————————————————————

    location-位置子對象


    document.location.hash // #號後的部分

    document.location.host // 域名+端口號

    document.location.hostname // 域名

    document.location.href // 完整URL

    document.location.pathname // 目錄部分

    document.location.port // 端口號

    document.location.protocol // 網絡協議(http:)

    document.location.search // ?號後的部分


    documeny.location.reload() //刷新網頁

    document.location.reload(URL) //打開新的網頁

    document.location.assign(URL) //打開新的網頁

    document.location.replace(URL) //打開新的網頁

    ———————————————————————

    selection-選區子對象document.selection

    ———————————————————————


    images集合(頁面中的圖像)


    a)通過集合引用document.images //對應頁面上的img標籤

    document.images.length //對應頁面上img標籤的個數

    document.images[0] //第1個img標籤

    document.images[i] //第i-1個img標籤


    b)通過nane屬性直接引用img

    name="oImage"document.images.oImage //document.images.name屬性


    c)引用圖片的src屬性

    document.images.oImage.src //document.images.name屬性.src


    d)創建一個圖像

    var oImageoImage = new Image()

    document.images.oImage.src="1.jpg"同時在頁面上建立一個img /標籤與之對應就可以顯示


    ———————————————————————


    forms集合(頁面中的表單)


    a)通過集合引用document.forms //對應頁面上的form標籤

    document.forms.length //對應頁面上/formform標籤的個數

    document.forms[0] //第1個/formform標籤

    document.forms[i] //第i-1個/formform標籤

    document.forms[i].length //第i-1個/formform中的控件數

    document.forms[i].elements[j] //第i-1個/formform中第j-1個控件


    b)通過標籤name屬性直接引用/formform name="Myform">input name="myctrl"/>/formdocument.Myform.myctrl //

    document.表單名.控件名


    c)訪問表單的屬性

    document.forms[i].name //對應form name>屬性

    document.forms[i].action //對應/formform action>屬性

    document.forms[i].encoding //對應/formform enctype>屬性

    document.forms[i].target //對應/formform target>屬性


    document.forms[i].appendChild(oTag) //動態插入一個控件

    document.all.oDiv //引用圖層oDiv

    document.all.oDiv.style.display="" //圖層設置為可視

    document.all.oDiv.style.display="none" //圖層設置為隱藏

    document.getElementId("oDiv") //通過getElementId引用對象

    document.getElementId("oDiv").style=""

    document.getElementId("oDiv").display="none"

    /*document.all表示document中所有對象的集合只有ie支持此屬性,因此也用來判斷瀏覽器的種類*/


    圖層對象的4個屬性

    document.getElementById("ID").innerText //動態輸出文本

    document.getElementById("ID").innerHTML //動態輸出HTML

    document.getElementById("ID").outerText //同innerText

    document.getElementById("ID").outerHTML //同innerHTML


    資料來源 : http://www.ccvita.com/80.html

    JavaScript 二維陣列

    //建立二維陣列

    function Array2DVar(x,y) { // 定義二維陣列原型

    this.length = x;

    this.x = x; // x 維度長度

    this.y = y; // y 維度長度

    for(var i = 0; i < this.length; i++) // 初始各元素值為 null

    this[i] = new Array(y); // this 代表物件本身

    }


    var AtlTab = new Array2DVar(2,2);

    var AtlArea = new Array2DVar(2,2);

    AtlTab[1][1] = 'AtlTab1';

    AtlTab[1][2] = 'AtlTab2';

    AtlArea[1][1] = 'AtlTop01';

    AtlArea[1][2] = 'AtlTop02';