简体   繁体   English

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

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

For example in the code below , I would like get the text Salutation of a the label tag with class description in the code below , tried to select the class but it's not working .. how can I select the text of the label uniquely according to the id of the label ? 例如,在下面的代码中,我想在下面的代码中获得带有类说明的标签标签的文本称呼,试图选择该类,但是它不起作用..如何根据以下内容唯一地选择标签的文本标签的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>

If you want to get it with ID , this is the way: $('#label_salutation_id').text() . 如果要使用ID来获取它,可以这样: $('#label_salutation_id').text()

You mention "with class description", then you can use this: $('label.description').text(); 您提到“带有描述”,然后就可以使用: $('label.description').text(); if you just have one <label> element with that class. 如果该类只有一个<label>元素。

Demo here 在这里演示

Even better : 更好的是

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

If you meant to select more than this one salutation, you can use: 如果您打算选择一种以上的称呼,则可以使用:

jQuery('label.description')

to select all label elements with the class description . 选择带有类description所有label元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM