Xin lỗi, nếu điều này hoàn toàn khớp với câu trả lời của bất kỳ ai ở trên,
$.fn.equalsText = function (text, isCaseSensitive) {
return $(this).filter(function () {
if (isCaseSensitive) {
return $(this).text() === text
} else {
return $(this).text().toLowerCase() === text.toLowerCase()
}
})
}
Đây là một số đầu ra trong bảng điều khiển trang kết quả tìm kiếm Linkedin.
$("li").equalsText("Next >", false)
[<li class="next">…</li>] // Output
$("li").equalsText("next >", false)
[<li class="next">…</li>] // Output
$("li").equalsText("Next >", true)
[<li class="next">…</li>] // Output
$("li").equalsText("next >", true)
[] // Output
Nó cũng có hỗ trợ phân biệt chữ hoa chữ thường và không sử dụng :contains()
Chỉnh sửa (ngày 22 tháng 5 năm 2017): -
$.fn.equalsText = function (textOrRegex, isCaseSensitive) {
return $(this).filter(function () {
var val = $(this).text() || this.nodeValue
if (textOrRegex instanceof RegExp) {
return textOrRegex.test(val)
} else if (isCaseSensitive) {
return val === textOrRegex
} else {
return val.toLowerCase() === textOrRegex.toLowerCase()
}
})
}