jQuery-如何查找在html注释中包含特定文本的html元素?

前端之家收集整理的这篇文章主要介绍了jQuery-如何查找在html注释中包含特定文本的html元素? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用jQuery来标识< td>包含特定HTML注释文本的元素.标准的“:contains”选择器似乎并没有考虑注释.我在这里的上下文是我想向同一td元素动态添加一些内容.这是目标td的示例:

<TD valign="top" class="ms-formbody" width="400px">
        <!-- FieldName="Title"
             FieldInternalName="Title"
             FieldType="SPFieldText"
          -->
            <span dir="none">
        <input name="ctl00$m$g_0b1e4ca3_9450_4f3e_b3af_8134fe014ea5$ctl00$ctl04$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField" type="text" maxlength="255" id="ctl00_m_g_0b1e4ca3_9450_4f3e_b3af_8134fe014ea5_ctl00_ctl04_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField" title="Title" class="ms-long" /><br>
    </span>


        </TD>

这个html是由SharePoint作为新列表项形式的一部分生成的,我真的很想在我的文档中找到其注释中的“ FieldInternalName”与某些文本字符串匹配的td.我如何使用jQuery(或任何JavaScript都可以)来最好地找到这些td元素?

最佳答案
您可以获取元素的innerHTML并在其上运行正则表达式.

例:

// HTML: <div id="myElement">Lorem <!-- a comment --> ipsum.</div>

var element = document.getElementById('myElement');
alert(element.innerHTML); // alerts "Lorem <!-- a comment --> ipsum."
// here you could run a regular expression

更新:一个更全面的示例(使用jQuery):

var tds = $('td'),match;
tds.each(function(index,element) {
    match = $(element).innerHTML.match(/FieldInternalName="(^["])"/);
    if (match) alert(match);
});
原文链接:https://www.f2er.com/html/530554.html

猜你在找的HTML相关文章