Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过类检查Jquery是否具有特定id进行搜索_Javascript_Jquery_Html - Fatal编程技术网

Javascript 通过类检查Jquery是否具有特定id进行搜索

Javascript 通过类检查Jquery是否具有特定id进行搜索,javascript,jquery,html,Javascript,Jquery,Html,嘿,我想搜索一个类并检查该类是否包含某个id。我必须使用每个id吗?如果我知道我不知道如何准确地使用它,有人能告诉我如何在这种情况下使用它吗, 谢谢你的帮助 if(id == $('.people').attr('id')) { alert("person exists"); } 您可以使用 $('#my-id') 就你而言 $('#' + id) 您可以通过测试长度来检查结果是否为空: if($('#'+id).length == 0) 要验证它是否是具有给定

嘿,我想搜索一个类并检查该类是否包含某个id。我必须使用每个id吗?如果我知道我不知道如何准确地使用它,有人能告诉我如何在这种情况下使用它吗, 谢谢你的帮助

  if(id == $('.people').attr('id'))
  {
       alert("person exists");
  }

您可以使用

$('#my-id')
就你而言

$('#' + id)
您可以通过测试
长度来检查结果是否为空:

if($('#'+id).length == 0)
要验证它是否是具有给定ID的
.person
元素,可以测试

if($('.person#'+id).length > 0) {
    alert('person exists');
}

您可以使用

$('#my-id')
就你而言

$('#' + id)
您可以通过测试
长度来检查结果是否为空:

if($('#'+id).length == 0)
要验证它是否是具有给定ID的
.person
元素,可以测试

if($('.person#'+id).length > 0) {
    alert('person exists');
}

由于ID不应被多次使用,您只需执行以下操作:

if($('#' + id + '.people').length >= 1)
  alert('person exists'); 

由于ID不应被多次使用,您只需执行以下操作:

if($('#' + id + '.people').length >= 1)
  alert('person exists'); 

由于id只能使用一次,因此您可以搜索具有该id的元素并检查其是否具有类。这将要快得多:

$('#the-id').hasClass('the-class');

由于id只能使用一次,因此您可以搜索具有该id的元素并检查其是否具有类。这将要快得多:

$('#the-id').hasClass('the-class');

一种解决方案是使用一个简单的选择器,并检查该元素是否存在

if ($('.people#'+id).length())  { //Checking length() checks if element exists
    alert("person exists");
}

一种解决方案是使用一个简单的选择器,并检查该元素是否存在

if ($('.people#'+id).length())  { //Checking length() checks if element exists
    alert("person exists");
}