简体   繁体   中英

JQuery Check if Div html has a certain character

I am trying to check with JQuery if a Div #id has a certain character or not.

Here's the code I'm trying:

if ($("#mydiv").html('0')) {

  alert'zero character was found';

}

The alert is not popping up.

I'm I doing it wrong?

尝试使用contains

$("#mydiv:contains(0)")
if ($("#mydiv:contains(0)")) {

  alert('zero character was found'); // do not forget also `(`

}

If I read your question correctly and you're looking for a div whose id contains the string 0 then this should work for you:

 if ($("div[id*='0']").length>0) {
     alert('zero character was found');
 }

http://jsfiddle.net/mT862/

try using native javascript

var str = $("#mydiv").text();
if (str.indexOf('0') !== -1) {
  alert('zero character was found');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM