2013-09-23 30 views
0

HTML:我可以知道這些警報之間的區別嗎?還需要關於attr的一些提示?

<div class="abc"> 
    <span data-rel="hai">Hello</span> 
    <span data-rel="bye">Bye</span> 
</div> 

的jQuery:

$(document).on('click','.abc >span',function(){ 
     alert(($(this).attr('data-rel')));     // This Code alerts hai. 
     alert($('.'+($(this).attr('data-rel'))+'_cls')); // I thought this code would alert as .hai_cls but it alerts as [object object]. 
}); 

我能知道這有什麼區別?

+0

爲什麼它會提醒同樣的事情?在一個提醒'rel'數據屬性的值中,一個提醒jQuery的值CSS類選擇器查找。兩個都有一對不必要的parens。 –

回答

2
alert( $('.'+($(this).attr('data-rel'))+'_cls') ); 

它調用的對象,因爲$(+「」在前面

alert(  $(this).attr('data-rel')+'_cls'  ); 

調用它,就像你想

+0

它解決了我的困惑..謝謝...我需要attr提示 – Nandakumar

1

第一個提醒ATTR值是對象名稱「hai」 第二個提醒類爲「hai_cls」的對象

相關問題