事件委托

(1)事件委托

  <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件委托</title>
<style type="text/css">
    .list{
        list-style: none;
    }
    .list li{
        height: 30px;
        background-color: green;
        margin-bottom: 10px;
        color: #fff;
    }
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(function(){
        /*
        給每個li綁定事件,一共綁定了8次婴程,性能不高
        $('.list li').click(function() {
            alert($(this).html());
        });
        */
        /*
        事件委托:方法delegate臣缀,只綁定一次事件校翔,冒泡觸發(fā)
            參數(shù):
                selector選擇器:寫入ul下面的所有要發(fā)生事件的元素桩盲,多個元素用空格隔開眯勾,例如‘li a span’
                eventType事件
                function要執(zhí)行的操作
        
        $('.list').delegate('li', 'click', function() {
            //$(this)指發(fā)生事件的子集吨拍,即每個li
            alert($(this).html());
            //取消委托
            $('.list').undelegate();
        });
    })
</script>
</head>
<body>
<ul class="list">
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
</ul>
</body>
</html>

(2)節(jié)點操作

  <!DOCTYPE html>
  <html lang="en">
  <head>
<meta charset="UTF-8">
<title>節(jié)點操作</title>
<style type="text/css">
    
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(function(){
        var $span = $('<span>span元素</span>');
        var $p = $('<p>p段落元素</p>');
        var $h = $('<h1>頁面標(biāo)題</h1>');
        /*插入子元素*/
        //div中插入span和p(末尾追加)
        // $('#div1').append($span);
        // $('#div1').append($p);
        // 把span和p插入div中
        $span.appendTo('#div1');
        $p.appendTo('#div1');
        //把子元素插入到父元素(前面追加)
        // prepend()
        // prependTo()
        //在div前邊插入兄弟h1標(biāo)題
        // $('#div1').before($h);
        $h.insertBefore('#div1');
        //在后邊插入兄弟元素
        //after()
        //insertAfter()
        //刪除p標(biāo)簽
        $p.remove();    
    })
    </script>
  </head>
<body>
<div id="div1"></div>
</body>
</html>

(3)ajax

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax</title>
<style type="text/css">
    
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $.ajax({
        url: 'data.json',//請求的服務(wù)器路徑褪猛,實際開發(fā)中寫文檔接口的路徑
        type: 'get',//分get/post請求
        dataType: 'json',//要讀取什么格式的數(shù)據(jù),xml script html upload
        // data:{page:1}//請求時要攜帶的參數(shù)
    })
    .done(function(data){//成功的時候會執(zhí)行的函數(shù)
        alert(data.name);
        console.log(data);
    })
    .fail(function(){//失敗的時候
        console.log("error");
    })
    /*.always(function(){//不論成功與否都會執(zhí)行
        console.log("always");
    })*/;
</script>
</head>
<body>

</body>
</html>

(4)jsonp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jsonp</title>
<style type="text/css">
    
</style>
<!-- <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> -->
<script type="text/javascript">
    // alert($);//function(a,b){return new n.fn.init(a,b)}
    /*
    jsonp可以跨域請求數(shù)據(jù)的原理:
        主要是利用了script標(biāo)簽可以跨域鏈接資源的特性
    */
    function aa(dat){
        alert(dat.name);
    }
</script>
<script type="text/javascript" src="js/data.js"></script>
</head>
<body>

</body>
</html>

(5)jQuery-jsonp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery-jsonp</title>
<style type="text/css">
    
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $.ajax({
        url: 'http://localhost:8080/1803/js/data.js',//跨域請求的地址羹饰,也可用相對路徑j(luò)s/data.js
        type: 'get',
        dataType: 'jsonp',//使用jsonp跨域請求
        jsonpCallback:'aa'
    })
    .done(function(data) {
        alert(data.name);
    })
    .fail(function() {
        console.log("error");
    });
</script>
</head>
<body>

</body>
</html>

(6)jsonp公開接口

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jsonp公開接口</title>
<style type="text/css">
    
</style>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    //360搜索的公開接口
    //https://sug.so.#/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=s
    $(function(){
        $('#txt01').keyup(function(){
            var val = $(this).val();
            $.ajax({
                url: 'https://sug.so.#/suggest?',//請求360搜索的公開接口
                type: 'get',
                dataType: 'jsonp',//跨域請求
                data: {word: val}//攜帶參數(shù)
            })
            .done(function(data) {
                console.log(data);
                // alert(data.s.length);//10條數(shù)據(jù)
                $('.list').empty();//先清空列表
                //模擬搜索聯(lián)想伊滋,循環(huán)插入新列表
                for(var i=0; i<data.s.length; i++){
                    var $li = $('<li>'+data.s[i]+'</li>');
                    $li.prependTo('.list');
                }
            })
            .fail(function() {
                console.log("error");
            });
        })
    })
    
</script>
</head>
<body>
<input type="text" id="txt01">
<ul class="list"></ul>
</body>
</html>

(7)

  /*
  NodeJS Static Http Server - http://github.com/thedigitalself/node-static-http-server/
  By James Wanga - The Digital Self
  Licensed under a Creative Commons Attribution 3.0 Unported License.
  A simple, nodeJS, http development server that trivializes serving static files.
  This server is HEAVILY based on work done by Ryan                   Florence(https://github.com/rpflorence) (https://gist.github.com/701407). I merged     this code with suggestions on handling varied MIME types found at Stackoverflow (http://stackoverflow.com/questions/7268033/basic-static-file-server-in-nodejs).
To run the server simply place the server.js file in the root of your web     application and issue the command 
$ node server.js 
or 
    $ node server.js 1234 
    with "1234" being a custom port number"
  Your web application will be served at http://localhost:8888 by default or     http://localhost:1234 with "1234" being the custom port you passed.
  Mime Types:
  You can add to the mimeTypes has to serve more file types.
  Virtual Directories:
  Add to the virtualDirectories hash if you have resources that are not children of the root directory
*/
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;

var mimeTypes = {
"htm": "text/html",
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"gif": "image/gif",
"js": "text/javascript",
"css": "text/css",  
"json":"text/json",  
"eot":"application/vnd.ms-fontobject",  
"svg":"image/svg+xml",  
"ttf":"application/octet-stream",  
"woff":"application/font-woff",  
"woff2":"application/font-woff"
  };

  var virtualDirectories = {
//"images": "../images/"
  };

  http.createServer(function(request, response) {

    var uri = url.parse(request.url).pathname
    , filename = path.join(process.cwd(), uri)
    , root = uri.split("/")[1]
    , virtualDirectory;

      virtualDirectory = virtualDirectories[root];
      if(virtualDirectory){
    uri = uri.slice(root.length + 1, uri.length);
    filename = path.join(virtualDirectory ,uri);
  }

    fs.exists(filename, function(exists) {
    if(!exists) {
  response.writeHead(404, {"Content-Type": "text/plain"});
  response.write("404 Not Found\n");
  response.end();
  console.error('404: ' + filename);
  return;
}

if (fs.statSync(filename).isDirectory()) filename += '/index.html';

fs.readFile(filename, "binary", function(err, file) {
  if(err) {        
    response.writeHead(500, {"Content-Type": "text/plain"});
    response.write(err + "\n");
    response.end();
    console.error('500: ' + filename);
    return;
  }

  var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
  response.writeHead(200, {"Content-Type": mimeType});
  response.write(file, "binary");
  response.end();
  console.log('200: ' + filename + ' as ' + mimeType);
});
  });
  }).listen(parseInt(port, 10));

  console.log("Static file server running at\n  => http://localhost:" + port + "/\nCTRL + C to shutdown")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市队秩,隨后出現(xiàn)的幾起案子友酱,更是在濱河造成了極大的恐慌释涛,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異椒振,居然都是意外死亡衔憨,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來使兔,“玉大人,你說我怎么就攤上這事藤韵∨傲ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵泽艘,是天一觀的道長欲险。 經(jīng)常有香客問我,道長匹涮,這世上最難降的妖魔是什么天试? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮焕盟,結(jié)果婚禮上秋秤,老公的妹妹穿的比我還像新娘。我一直安慰自己脚翘,他們只是感情好灼卢,可當(dāng)我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著来农,像睡著了一般鞋真。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上沃于,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天涩咖,我揣著相機與錄音,去河邊找鬼繁莹。 笑死檩互,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的咨演。 我是一名探鬼主播闸昨,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼薄风!你這毒婦竟也來了饵较?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤遭赂,失蹤者是張志新(化名)和其女友劉穎循诉,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體撇他,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡茄猫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年狈蚤,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片募疮。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡炫惩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出阿浓,到底是詐尸還是另有隱情,我是刑警寧澤蹋绽,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布芭毙,位于F島的核電站,受9級特大地震影響卸耘,放射性物質(zhì)發(fā)生泄漏退敦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一蚣抗、第九天 我趴在偏房一處隱蔽的房頂上張望侈百。 院中可真熱鬧,春花似錦翰铡、人聲如沸钝域。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽例证。三九已至,卻和暖如春迷捧,著一層夾襖步出監(jiān)牢的瞬間织咧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工漠秋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留笙蒙,地道東北人。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓庆锦,卻偏偏與公主長得像捅位,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子肥荔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容

  • (1)事件委托 (2)節(jié)點操作 (3)ajax (4)jsonp (5)jQuery-jsonp (6)jsonp...
    HDhandi閱讀 306評論 0 0
  • (1)事件委托 (2)節(jié)點操作 (3)ajax (4)jsonp (5)jQuery-jsonp (6)jsonp...
    松雪寶寶閱讀 431評論 0 3
  • 1.背景介紹 1.1什么是事件委托绿渣? 事件委托還有一個名字叫事件代理,JavaScript高級程序設(shè)計上講:事件委...
    我叫于搞吧閱讀 1,647評論 4 9
  • 大家好燕耿,我是IT修真院成都分院第07期學(xué)員中符,一枚正直善良的web程序員。 一誉帅、小課堂簡述JS中的事件委托 1.背景...
    120De丶L閱讀 330評論 0 0
  • 離2018年的CATTI考試只剩150天淀散,既然我下定決心再考一次(多年前考過一次右莱,只過了一門。)档插,那就必須積極備考...
    三七木木閱讀 1,477評論 0 3