如何查找包含特定文本的HTML注释中的HTML元素?

3

我尝试使用jQuery来识别包含特定HTML注释文本的<td>元素。标准的“: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元素?

2个回答

3
你可以获取元素的 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);
});

0
if(document.getElementById('myElement').innerHTML.indexOf(text_string) != -1) { //do something }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接