开发者

jquery: how to find a single character in html

I'm looking for a way to det开发者_开发技巧ect wheter my label object has a "-" character in it.

<label>blabla</label>  -> has not
<label>bla-bla</label> -> has

Im not to familiar with regular expressions.


First get the contents of the label, then use the regex /-/.

> "blabla".match(/-/)
> null

> "bla-bla".match(/-/)
> ["-"]

You don't even need a regex here, you can use indexOf:

> "blabla".indexOf('-')
> -1

> "bla-bla".indexOf('-')
> 3

If you just want to select the label element if it contains a "-", you could use:

$('label:contains("-")')

It depends on what you are trying to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜