JavaScript - 检查此(当前元素)是否具有以.结尾的类

Ila*_*sda 1 javascript jquery jquery-selectors

要选择以-topI 结尾的输入,可以起诉以下内容:

$("input[class$='-top'],input[class*='-top ']")

但是,如何检查这是否结束-top

$('Input').keyup( function(){

 // How to check if $(this).prop('class') end with  "-top"

});
Run Code Online (Sandbox Code Playgroud)

如何检查 $(this).prop('class') end with "-top"

任何建议非常感谢.

Poi*_*nty 7

您正在寻找.is():

if ($(this).is('[class*=-top]')) { ... }
Run Code Online (Sandbox Code Playgroud)

编辑 - 在您选择的选择器中替换当然...

  • 你必须将选择器改为`[class $ = - top],[class*= - top]`.否则,`class =" - top-not-ending"`也匹配. (2认同)