$.data($(this),'key1',"name");為什么這么寫(xiě)不行的,而是要賦值?
?$(".left").click(function(){
? ? ? ? ? ?//給rleft增加數(shù)據(jù)存儲(chǔ)
? ? ? ? ? ? $.data($(this),'key1',"name");
? ? ? ? ? ? //獲取數(shù)據(jù)
? ? ? ? ? ? var text = $.data($(this),"key1");
? ? ? ? ? ? $("span").text(text);
? ? ? ? })
為什么這么寫(xiě)沒(méi)內(nèi)容輸出,而是要賦值?
div>
? ? <script type="text/javascript">
? ? ? ? $(".left").click(function(){
? ? ? ? ? ?var ?$this = $(this);
? ? ? ? ? ?//給rleft增加數(shù)據(jù)存儲(chǔ)
? ? ? ? ? ? $.data($this,'key1',"name");
? ? ? ? ? ? //獲取數(shù)據(jù)
? ? ? ? ? ? var text = $.data($this,"key1");
? ? ? ? ? ? $("span").text(text);
? ? ? ? })
? ? </script>
2017-03-01
關(guān)鍵字 this 總是指向調(diào)用該方法的對(duì)象
第一個(gè)this是指.left這個(gè)節(jié)點(diǎn)
第二個(gè)this是指$.data($(this),'key1',"name");
this是不斷變化的,必須用變量存儲(chǔ)起來(lái)
2017-02-28
$.data($(this), "a", "data test");這句話(huà)里的$(this)應(yīng)該代表的是$,也就是jQuery,而不是?$(".left")這個(gè)元素.
$('.right').click(function() {
? ?var ele = $(this);
? ?//通過(guò).data方式設(shè)置數(shù)據(jù)
? ?$(this).data("a", "data test")
? ?$(this).data("b", {
? ? ? ?name: "慕課網(wǎng)"
? ?})
? ?//通過(guò).data方式取出數(shù)據(jù)
? ?var reset = $(this).data("a") + "</br>" + $(this).data("b").name
? ?ele.find('span').append(reset)
})
這個(gè)里面的$(this)指的就是$('.right')這個(gè)個(gè)元素,?$(this).data("a", "data test")數(shù)據(jù)也是存儲(chǔ)在它下面的的。