切换元素的隐藏标签

时间:2011-04-13 17:44:49

标签: javascript jquery label toggle

如何检查元素是否有标签并调用切换功能来显示它?

试试这个:

if($('label').attr('for') == $(this).attr('name')) {
    alert($('label').attr('for') == $(this).attr('name')); // displays true if hidden or displayed
    $('label').attr('for='+$(this).attr('name')).toggle(true); // error
}

HTML未隐藏(这只是为了在隐藏元素时显示正确的语法):

<div>
    <label for="state" class="ui-select">
        State*
    </label>
    <div class="ui-select">
        <a href="#" role="button" aria-haspopup="true" data-theme="z" class="ui-btn ui-btn-icon-right ui-btn-corner-all ui-shadow ui-btn-down-z ui-btn-up-z">
        <span class="ui-btn-inner ui-btn-corner-all">
            <span class="ui-btn-text">State*</span>
            <span class="ui-icon ui-icon-arrow-d ui-icon-shadow"></span>
        </span>
        </a>
        <select name="state" id="state" tabindex="-1" class="required">
            <option value="">State*</option>
            <option value="AK">ALASKA</option>
            <option value="...">...</option>
        </select>
    </div>
</div>

HTML隐藏:

<div>       
    <div class="ui-select">
        <a href="#" role="button" aria-haspopup="true" data-theme="z" class="ui-btn ui-btn-icon-right ui-btn-corner-all ui-shadow ui-btn-down-z ui-btn-up-z">
        <span class="ui-btn-inner ui-btn-corner-all">
            <span class="ui-btn-text">State*</span>
            <span class="ui-icon ui-icon-arrow-d ui-icon-shadow"></span>
        </span>
        </a>
        <select name="state" id="state" tabindex="-1" class="required">
            <option value="">State*</option>
            <option value="AK">ALASKA</option>
            <option value="...">...</option>
        </select>
    </div>
</div>

1 个答案:

答案 0 :(得分:5)

听起来你正在寻找$('label[for="' + this.name + '"]') 您可以使用Attribute Equals selector选择具有特定属性值的元素。