Javascript Jquery 从跨度中删除文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11207432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:54:16  来源:igfitidea点击:

Jquery remove text from span

javascriptjquery

提问by Cann0nF0dder

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

我是 JQuery 的新手,并且真的在努力如何执行我想要的查询。

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.

我正在使用 SharePoint,并且在使用外部数据列并将其设置为必填字段时,错误消息始终出现,直到您填写数据。与其他类型的列不同,它们仅在您单击“确定/保存”后才出现,而不是填写数据。因此,我需要从这些类型的列中删除错误消息文本。

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

我假设我需要在包含“外部数据”字样的 .ms-error 类中搜索 span 并将其隐藏。

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

通过使用 IE 开发人员工具栏,我已经确定了该区域。

 <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.

请有人可以帮助我使用 JQuery。

采纳答案by Moin Zaman

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

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

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

如果您确定这些span's 在某个table或 a 内,div则将其专门定位在那些内以使脚本执行得更好。

eg.

例如。

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

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

回答by jbabey

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

回答by Ekas Preet Singh

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-error-1 .ms-error-abc。因此,相同的语法有效,甚至更好,您可以这样做:

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