使用jquery访问具有相同类名但不同测试的嵌套div

时间:2022-04-21 12:20:35

I want to iterate through the div structure.Actually, what I want is if I have different div structures with same class name e.g. mod and I want to check the internal div whose class name is title. The only difference between them are the contents of the div with class name title have text as hello1 and for other its hello2.

我想迭代div结构。实际上,我想要的是如果我有不同的具有相同类名的div结构,例如mod和我想检查其类名是title的内部div。它们之间的唯一区别是具有类名称标题的div的内容将文本设置为hello1,将其他文件设置为hello2。

Here is the Structure

这是结构

<div class="mod" id="mod23" >
      <div class="content" >
       <div class="hd" >
         <div class="title">Hello1</div>
           <ul class="list"></ul>
           <ul class="buttons">
             <li class="help"></li>
             <li class="show" ></li>
             <li class="close"></li>
           </ul>
       </div>
      </div>
     </div>
    <div class="mod" id="mod27" >
      <div class="content" >
       <div class="hd" >
         <div class="title">Hello2</div>
           <ul class="list"></ul>
           <ul class="buttons">
             <li class="help"></li>
             <li class="show" ></li>
             <li class="close"></li>
           </ul>
       </div>
      </div>
     </div>

Here is the code I have tried to come up with which is not working

这是我试图提出的代码,它不起作用

$('div').each(function(index) {
if($(this).hasClass('title').text('Hello1')){
    alert('found');
    }
});

6 个解决方案

#1


3  

you can use contains selector:

你可以使用包含选择器:

$('div.title:contains("Hello")').each(function(index) {
    alert('found');
});

http://jsbin.com/atayeb/2/

http://jsbin.com/atayeb/2/

#2


3  

var result = $('div.title').filter(function() {
    return $(this).text() === "Hello1";
});

// Do something with result

Or you can look at the contains-selector but this does match everything witch matches your query.

或者您可以查看包含选择器,但这确实匹配与您的查询匹配的所有内容。

var result = $('div.title:contains(Hello1)')

To check if you match anything simply:

要检查您是否匹配任何内容:

if ( result.length ) {
    alert("found");
}

#3


2  

$('div').each(function(index) {
if($(this).find('.title').first().text() == 'Hello1'){
    alert('found');
    }
});

there might be cooler ways though

可能会有更酷的方式

#4


2  

Try this instead:

试试这个:

<script>
$('div').each(function(index) {
if($(this).hasClass('title') && $(this).text() == 'Hello1'){
    alert('found');
    }
});
</script>

Good luck!

祝你好运!

#5


2  

You can get to the div you are interested in more directly by using a CSS selector:

您可以使用CSS选择器更直接地访问您感兴趣的div:

$('.mod .title').each(function(i, e) {  
    if($(e).text() == 'Hello1') {
        alert('found');
    }
});?

#6


1  

 $('div').each(function (index) {
        if ($(".title")) {
            if ($(this).text('Hello1')) {
                alert('found');
            }
        }
    });

#1


3  

you can use contains selector:

你可以使用包含选择器:

$('div.title:contains("Hello")').each(function(index) {
    alert('found');
});

http://jsbin.com/atayeb/2/

http://jsbin.com/atayeb/2/

#2


3  

var result = $('div.title').filter(function() {
    return $(this).text() === "Hello1";
});

// Do something with result

Or you can look at the contains-selector but this does match everything witch matches your query.

或者您可以查看包含选择器,但这确实匹配与您的查询匹配的所有内容。

var result = $('div.title:contains(Hello1)')

To check if you match anything simply:

要检查您是否匹配任何内容:

if ( result.length ) {
    alert("found");
}

#3


2  

$('div').each(function(index) {
if($(this).find('.title').first().text() == 'Hello1'){
    alert('found');
    }
});

there might be cooler ways though

可能会有更酷的方式

#4


2  

Try this instead:

试试这个:

<script>
$('div').each(function(index) {
if($(this).hasClass('title') && $(this).text() == 'Hello1'){
    alert('found');
    }
});
</script>

Good luck!

祝你好运!

#5


2  

You can get to the div you are interested in more directly by using a CSS selector:

您可以使用CSS选择器更直接地访问您感兴趣的div:

$('.mod .title').each(function(i, e) {  
    if($(e).text() == 'Hello1') {
        alert('found');
    }
});?

#6


1  

 $('div').each(function (index) {
        if ($(".title")) {
            if ($(this).text('Hello1')) {
                alert('found');
            }
        }
    });