Javascript 通过 div 中的元素递增?

标签 javascript jquery html dom increment

好的,我有以下 HTML:

<div class="calculator-section">
    <p class="x"></p>
    <p class="y"></p>
</div>

<div class="calculator-section">
    <p class="z"></p>
    <p class="a"></p>
</div>

<div class="calculator-section">
    <p class="b"></p>
    <p class="c"></p>
</div>

我需要递增这些 div 中的每一个并比较每个 <p> 的类。有。

我该如何去做呢?

目前我有这个:

$('.calculator-section').each(function(i, obj) {
    $(this).$('p').each(function(i, obj) { //This bit doesn't work
        //Check classes for $(this) here?
    });
});

但我不确定该内部循环该怎么做。有什么想法吗?

最佳答案

p 添加到初始的每个循环中,并在循环中使用 className 属性

$('.calculator-section p').each(function(i, obj) {
    if(this.className == "x") {
       $(this).css('background', 'green')
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="calculator-section">
    <p class="x">x</p>
    <p class="y">y</p>
</div>  

<小时/> 或者,如果您出于某种原因使用多个循环:

使用 $('p', this)$(this).children('p')< 在特定部分的迭代中选择 p/

$('.calculator-section').each(function(i, obj) {
    // $(this) = section
    $('p', this).each(function(i, obj) {     
        // $(this) = p within above section                
    });  
});

关于Javascript 通过 div 中的元素递增?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075199/

相关文章:

javascript - js如何改变鼠标悬停时矩形的颜色?

c# - 将 JSON 值传递给 Web 方法返回 'Unknown web method DoesUserExist. Parameter name: methodName' 错误

HTML 表格不同行中的不同列数

python - 使用其他表单元素在 Flask 中上传文件失败并出现 400 错误

html - XAMPP:访问根目录子目录中的html页面

javascript - 如何在html中显示通过ajax传递的数组

javascript - 复选框颜色变为灰色

javascript - 如何从 JOptionPane.showMessageDialog 读取

jquery - 单击链接时显示 div 的问题

jquery - 无法跨浏览器获得日历的统一样式