ขออภัยหากตรงกับคำตอบของทุกคนด้านบน
$.fn.equalsText = function (text, isCaseSensitive) {
return $(this).filter(function () {
if (isCaseSensitive) {
return $(this).text() === text
} else {
return $(this).text().toLowerCase() === text.toLowerCase()
}
})
}
นี่คือผลลัพธ์บางส่วนในคอนโซลหน้าผลการค้นหา Linkedin
$("li").equalsText("Next >", false)
[<li class="next">…</li>]
$("li").equalsText("next >", false)
[<li class="next">…</li>]
$("li").equalsText("Next >", true)
[<li class="next">…</li>]
$("li").equalsText("next >", true)
[]
มีการรองรับกรณีไวและไม่ได้ใช้งาน :contains()
แก้ไข (22 พฤษภาคม 2560): -
$.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()
}
})
}