您尝试获取列表项的属性,只需在.on()中调用它:
$('#iti-item-dz').attr('data-dial-code');
只需使用其中的父类 ul 标签和使用 this 获取点击项目值的上下文。 并且您将通过单击项目获取所有项目列表数据。
ul
this
运行以下代码段
$('.country-list .country').on('click', function() { var country = $(this).find('.country-name').text(); alert(country); });
$(document).ready(function() { $('.country-list .country').on('click', function() { var country = $(this).find('.country-name').text(); alert(country); }); })
.country { display: block; padding: 5px; background: gray; cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="country-list hide" id="country-listbox" aria-expanded="false" role="listbox" aria-activedescendant="iti-item-al"> <li class="country preferred" id="iti-item-fr" role="option" data-dial-code="33" data-country-code="fr" aria-selected="false"> <div class="flag-box"> <div class="iti-flag fr"></div> </div><span class="country-name">France</span><span class="dial-code">+33</span></li> <li class="divider" role="separator" aria-disabled="true"></li> <li class="country standard" id="iti-item-af" role="option" data-dial-code="93" data-country-code="af"> <div class="flag-box"> <div class="iti-flag af"></div> </div><span class="country-name">Afghanistan (????????????)</span><span class="dial-code">+93</span></li> <li class="country standard highlight active" id="iti-item-al" role="option" data-dial-code="355" data-country-code="al" aria-selected="true"> <div class="flag-box"> <div class="iti-flag al"></div> </div><span class="country-name">Albania (Shqip?ri)</span><span class="dial-code">+355</span></li> <li class="country standard" id="iti-item-dz" role="option" data-dial-code="213" data-country-code="dz"> <div class="flag-box"> <div class="iti-flag dz"></div> </div><span class="country-name">Algeria (??????????)</span><span class="dial-code">+213</span></li> <li class="country standard" id="iti-item-as" role="option" data-dial-code="1684" data-country-code="as"> <div class="flag-box"> <div class="iti-flag as"></div> </div><span class="country-name">American Samoa</span><span class="dial-code">+1684</span></li> <li class="country standard" id="iti-item-ad" role="option" data-dial-code="376" data-country-code="ad"> <div class="flag-box"> <div class="iti-flag ad"></div> </div><span class="country-name">Andorra</span><span class="dial-code">+376</span></li> <li class="country standard" id="iti-item-ao" role="option" data-dial-code="244" data-country-code="ao"> <div class="flag-box"> <div class="iti-flag ao"></div> </div><span class="country-name">Angola</span><span class="dial-code">+244</span></li> <li class="country standard" id="iti-item-ai" role="option" data-dial-code="1264" data-country-code="ai"> <div class="flag-box"> <div class="iti-flag ai"></div> </div><span class="country-name">Anguilla</span><span class="dial-code">+1264</span></li> <li class="country standard" id="iti-item-ag" role="option" data-dial-code="1268" data-country-code="ag"> <div class="flag-box"> <div class="iti-flag ag"></div> </div><span class="country-name">Antigua and Barbuda</span><span class="dial-code">+1268</span></li> <li class="country standard" id="iti-item-ar" role="option" data-dial-code="54" data-country-code="ar"> <div class="flag-box"> <div class="iti-flag ar"></div> </div><span class="country-name">Argentina</span><span class="dial-code">+54</span></li> <li class="country standard" id="iti-item-am" role="option" data-dial-code="374" data-country-code="am"> <div class="flag-box"> <div class="iti-flag am"></div> </div><span class="country-name">Armenia (????????)</span><span class="dial-code">+374</span></li> <li class="country standard" id="iti-item-aw" role="option" data-dial-code="297" data-country-code="aw"> <div class="flag-box"> <div class="iti-flag aw"></div> </div><span class="country-name">Aruba</span><span class="dial-code">+297</span></li> <li class="country standard" id="iti-item-au" role="option" data-dial-code="61" data-country-code="au"> <div class="flag-box"> <div class="iti-flag au"></div> </div><span class="country-name">Australia</span><span class="dial-code">+61</span></li> <li class="country standard" id="iti-item-at" role="option" data-dial-code="43" data-country-code="at"> <div class="flag-box"> <div class="iti-flag at"></div> </div><span class="country-name">Austria (?sterreich)</span><span class="dial-code">+43</span></li> <li class="country standard" id="iti-item-az" role="option" data-dial-code="994" data-country-code="az"> <div class="flag-box"> <div class="iti-flag az"></div> </div><span class="country-name">Azerbaijan (Az?rbaycan)</span><span class="dial-code">+994</span></li> <li class="country standard" id="iti-item-bs" role="option" data-dial-code="1242" data-country-code="bs"> <div class="flag-box"> <div class="iti-flag bs"></div> </div><span class="country-name">Bahamas</span><span class="dial-code">+1242</span></li> <li class="country standard" id="iti-item-bh" role="option" data-dial-code="973" data-country-code="bh"> <div class="flag-box"> <div class="iti-flag bh"></div> </div><span class="country-name">Bahrain (??????????)</span><span class="dial-code">+973</span></li> <li class="country standard" id="iti-item-bd" role="option" data-dial-code="880" data-country-code="bd"> <div class="flag-box"> <div class="iti-flag bd"></div> </div><span class="country-name">Bangladesh (????????)</span><span class="dial-code">+880</span></li> <li class="country standard" id="iti-item-bb" role="option" data-dial-code="1246" data-country-code="bb"> <div class="flag-box"> <div class="iti-flag bb"></div> </div><span class="country-name">Barbados</span><span class="dial-code">+1246</span></li> <li class="country standard" id="iti-item-by" role="option" data-dial-code="375" data-country-code="by"> <div class="flag-box"> <div class="iti-flag by"></div> </div><span class="country-name">Belarus (���֧ݧѧ����)</span><span class="dial-code">+375</span></li> <li class="country standard" id="iti-item-be" role="option" data-dial-code="32" data-country-code="be"> <div class="flag-box"> <div class="iti-flag be"></div> </div><span class="country-name">Belgium (Belgi?)</span><span class="dial-code">+32</span></li> <li class="country standard" id="iti-item-bz" role="option" data-dial-code="501" data-country-code="bz"> <div class="flag-box"> <div class="iti-flag bz"></div> </div><span class="country-name">Belize</span><span class="dial-code">+501</span></li> <li class="country standard" id="iti-item-bj" role="option" data-dial-code="229" data-country-code="bj"> <div class="flag-box"> <div class="iti-flag bj"></div> </div><span class="country-name">Benin (B��nin)</span><span class="dial-code">+229</span></li> <li class="country standard" id="iti-item-bm" role="option" data-dial-code="1441" data-country-code="bm"> <div class="flag-box"> <div class="iti-flag bm"></div> </div><span class="country-name">Bermuda</span><span class="dial-code">+1441</span></li> <li class="country standard" id="iti-item-bt" role="option" data-dial-code="975" data-country-code="bt"> <div class="flag-box"> <div class="iti-flag bt"></div> </div><span class="country-name">Bhutan (?????)</span><span class="dial-code">+975</span></li> <li class="country standard" id="iti-item-eg" role="option" data-dial-code="20" data-country-code="eg"> <div class="flag-box"> <div class="iti-flag eg"></div> </div><span class="country-name">Egypt (??????)</span><span class="dial-code">+20</span></li> <li class="country standard" id="iti-item-sv" role="option" data-dial-code="503" data-country-code="sv"> <div class="flag-box"> <div class="iti-flag sv"></div> </div><span class="country-name">El Salvador</span><span class="dial-code">+503</span></li> </ul>