Jquery remove text from span

2020-05-24 04:45发布

I'm new to JQuery and really struggling how to perform the query I want.

I'm using SharePoint, and the when using the External Data Column and set it to required field, the error message always appears until you fill in the data. Unlike other type columns where they only appear after you click OK/Save and not filled in the data. Therefore I need to remove the error message text just from these type of columns.

I assume I need to search for span within the .ms-error class that contains the words 'External Data' and hide it.

From using IE developer toolbar, I've identified the area.

 <table class="ms-usereditor" id="ctl00_m_g_05df537a_596b_443a_a11e_764069facec8_ctl00_Field_External_539c53fe_8334_43c8_b089_cc28d7895e68_Picker_OuterTable" style="border-collapse: collapse;" border="0" cellSpacing="0" cellPadding="0">
    <tbody>
     <tr>
     <td colSpan="3">
       <span class="ms-error" id="ctl00_m_g_05df537a_596b_443a_a11e_764069facec8_ctl00_Field_External_539c53fe_8334_43c8_b089_cc28d7895e68_Picker_errorLabel">
           Text - You must specify a value before using the Check button. You can also use Select button to choose External Data.
        </span>
      </td>
      </tr>
    </tbody>
</table>

Please can someone help me with the JQuery.

3条回答
混吃等死
2楼-- · 2020-05-24 05:35
var spans = $('.ms-error');

spans.text(''); // clear the text
spans.hide(); // make them display: none
spans.remove(); // remove them from the DOM completely
spans.empty(); // remove all their content
查看更多
Summer. ? 凉城
3楼-- · 2020-05-24 05:40

Here's more optimised and faster approach:

$(".ms-usereditor span[class^='ms-error']:contains('External Data')").hide()

And additionally, this syntax works as a charm when you require a sort of regex pattern to find all matching nodes or nodes with similar classnames. Suppose, you need to find something .ms-error-1 .ms-error-abc. So, same syntax works and even better you could do like this:

$(".ms-usereditor span[class^='ms-error-']:contains('External Data')").hide()
查看更多
狗以群分
4楼-- · 2020-05-24 05:44

$('span.ms-error:contains("External Data")').hide();

If you know for sure that these span's are inside a certain table or a div then target it specifically inside those to make the script perform better.

eg.

$('.ms-usereditor span.ms-error:contains("External Data")').hide();

查看更多
登录 后发表回答