1

下面是我的代码

function getName(selectName, selectedValue) {
    var partices = $('#dk_container_'+selectName);
    var selectlbl = partices.find('.dk_label');
    var ul = partices.find('.dk_options_inner');

    $('li').each(function () {
        if ($('a', this).text() == selectedValue) {
            $(this).addClass('dk_option_current');
            selectlbl.html(selectedValue);
        }
        else {
            $(this).attr('class', '');
        }
    });
}

问题是$(this).addClass('dk_option_current');。此特定行旨在在满足 if 条件dk_option_current的元素中插入一个类。liselectlbl.html(selectedValue);条线工作正常。但是我想添加的类根本没有相应地添加。如有不妥请指出。

HTML

 <a class="dk_toggle" style="width: 300px;">
    <span class="dk_label"></span>
    </a>
    <div class="dk_options">
    <ul class="dk_options_inner">
    <ul class="dk_options_inner">
<li class="">
<a data-dk-dropdown-value=""> </a>
</li>
<li class="">
<a data-dk-dropdown-value="val1">Text1</a>
</li>
<li class="">
<a data-dk-dropdown-value="val2">Text2</a>
</li>
<li class="">
<a data-dk-dropdown-value="val3">Text3</a>
</li>
    </ul>
    </div>
4

1 回答 1

1

尝试这个:

function getName(selectName, selectedValue)
{
var partices = $('#dk_container_'+selectName);
 var selectlbl=partices.find('.dk_label');
 var ul=partices.find('.dk_options_inner');
 $('li').each(function () {
    var elem = $(this);
    if($('a',this).text()== selectedValue){

      elem.addClass('dk_option_current');
      selectlbl.html(selectedValue);
    }
    else{
      elem.attr('class','');
   }
 });
}
于 2013-01-30T10:49:44.280 回答