如果你想要更通用的东西,那么你可以使用这样的东西。
HTML
<table>
<tbody>
<tr>
<td><p><b>Newsletter Issue Date: June 10th, 2013</b></p></td>
<td><p><b>Newsletter Issue Date: June 11th, 2013</b></p></td>
<td><p><b>Newsletter Issue Date: June 12th, 2013</b></p></td>
</tr>
<tr>
<td><p><b>Newsletter Issue Date: June 13th, 2013</b></p></td>
<td><p><b>Newsletter Issue Date: June 24th, 2013</b></p></td>
<td><p><b>Newsletter Issue Date: June 25th, 2013</b></p></td>
</tr>
<tr>
<td><p><b>Newsletter Issue Date: June 20th, 2013</b></p></td>
<td><p><b>Newsletter Issue Date: June 21st, 2013</b></p></td>
<td><p><b>Newsletter Issue Date: June 22nd, 2013</b></p></td>
</tr>
</tbody>
</table>
Javascript
/*jslint maxerr: 50, indent: 4, browser: true */
(function () {
"use strict";
function walkTheDOM(node, func) {
if (node && node.nodeType) {
if (typeof func === "function") {
func(node);
}
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
}
function escapeRegex(string) {
return string.replace(/[\[\](){}?*+\^$\\.|]/g, "\\$&");
}
function filterElementsByContains(elements, string) {
var toStringFN = {}.toString,
text = toStringFN.call(elements),
result,
length,
i,
element;
if (text !== "[object NodeList]" && text !== "[object Array]" && !($() instanceof jQuery)) {
return result;
}
result = [];
if (typeof string === "string") {
string = new RegExp("^" + escapeRegex(string) + "$");
} else if (toStringFN.call(string) !== "[object RegExp]") {
return result;
}
function getText(node) {
if (node.nodeType === 3) {
text += node.nodeValue;
}
}
length = elements.length;
i = 0;
while (i < length) {
text = "";
element = elements[i];
walkTheDOM(element, getText);
if (string.test(text)) {
result.push(element);
}
i += 1;
}
return result;
}
if (filterElementsByContains([document.getElementsByTagName("table")[0]], /June 20/).length) {
alert("exists");
}
}());
在jsfiddle 上
您可以在此答案中看到其使用的其他一些示例
如果您必须将 XPATH 转换为 CSS 选择器,请查看此GIST