在这种特殊情况下,如何使用Jquery获取标签的文本

How can I get the text of a label using Jquery in this particular case ?

本文关键字:获取 Jquery 标签 文本 何使用 特殊情况下      更新时间:2023-09-26

例如,在下面的代码中,我想在下面的码中获得一个带有类描述的标签标签的文本Salutation,试图选择该类,但它不起作用。。如何根据标签的id唯一地选择标签的文本?

<tr id="salutation_id" class="edit_tr" border="0">
    <td class="edit_td">
    <div id="salutation_id" class="unique_ids database_key">
    <span id="first_salutation_id">
    <h1 class="ui-widget-header">
    <label id="label_salutation_id" class="description">Salutation</label>
    </h1>
    </span>
    <input id="first_input_salutation_id" class="editbox" type="text" value="Salutation" style="display: none;">
    <div class="ui-widget-content">
    <ol class="ui-droppable ui-sortable" style="">
    <li class="placeholder">Add "Salutation" here</li>
    </ol>
    </div>
    </div>
    </td>
    </tr>

如果你想用ID获取它,这是一种方法:$('#label_salutation_id').text()

如果您提到"with class description",那么您可以使用这个:$('label.description').text();,如果该类只有一个<label>元素。

演示此处

甚至更好:

var label = document.getElementById('label_salutation_id'),
    text = label.textContent || label.innerText;

如果你想选择不止一个称呼,你可以使用:

jQuery('label.description')

以选择具有类别CCD_ 5的所有CCD_。