如何在html注释中找到包含特定文本的html元素? [英] How can I find html elements that contain specific text in an html comment?
问题描述
我想使用jQuery来识别< td>包含特定HTML注释文本的元素。标准的:contains选择器似乎没有查看评论。我的上下文是,我想动态添加一些内容到同一个td元素。这里是目标td的示例:
I'm trying to use jQuery to identify <td> elements which contain specific HTML comment text. The standard ":contains" selector doesn't seem to be looking into the comments. My context here is that I want to dynamically add some content to that same td element. Here's a sample of the target 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生成的一个新的列表项表单的一部分,我真的想找到在我的文档中的td,其中FieldInternalName在注释中匹配一些文本字符串。
This html is generated by SharePoint as part of a new list item form, and I really want to find the td in my document whose "FieldInternalName" in the comment matches some text string. How can I use jQuery (or any javascript would really be okay) to best find these td elements?
推荐答案
你可以使用jQuery(或任何javascript会真的会好起来)找到这些td元素。 innerHTML
,并在其上运行正则表达式。
You could grab the innerHTML
of the element and run a regular expression over it.
示例:
// 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):
Update: a more comprehensive example (with jQuery):
var tds = $('td'), match;
tds.each(function(index, element) {
match = $(element).innerHTML.match(/FieldInternalName="(^["])"/);
if (match) alert(match);
});
这篇关于如何在html注释中找到包含特定文本的html元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!