jquery 选择器 : child elements of the same class inside divs of different classes

标签 jquery html css jquery-selectors

有两个类为“.div1”和“.div2”的div。 它们中的每一个都包含相同的代码——具有相同类名的相同元素。 如果我需要在其中一个 div 中设置/获取某个元素的值,我该如何引用这些元素? 我尝试以下操作:

var hostPopup = "hostdiv" + selectedPopup + " "; // where selectedPopup = "1" or "2"
$(hostPopup + '.testColor').css('background-color', rgba);
$(hostPopup + '.testColorText').css({'color': rgba, '-webkit-filter': 'invert(100%)', 'filter': 'invert(100%)'});

它不起作用。但是,如果我使用 hostPopup 仅为一个操作指定主机 div - 这适用于此处的第一行:

$(hostPopup + '.testColor').css('background-color', rgba);
$('.testColorText').css({'color': rgba, '-webkit-filter': 'invert(100%)', 'filter': 'invert(100%)'});

有效!

这是我的一段 html 的示例:

<div class="hostdiv1" style="padding: 0;"> <!-- hostdiv2 has exactly the same code in it -->
    <div class="NamesDiv nameTypesDiv" style="background-color: dimgray;">
    <div class="namesClass">
        <b>1.</b> Add names
    </div>
    <div class="testColor" style="background-color: rgb(124,124,124); color: white;"><p class="testColorText">Test</p></div>
    <div class="rangeDiv" style="height: 120px;">
    <div class="rangeBars"><input type="range" class="rangeR" min="0" max="100" value="50" tabindex="0"></input>
    <output for="rangeR" class="outR outRange">50</output></div>

那么,为什么 $(".nodeDiv1class .itsinnerelementclass").'action'... 不希望使用位于类为 nodeDiv1class 的元素内部某处的 itsinnerelementclass 元素?

最佳答案

我认为您需要在 hostPopup 的开头添加一个点,以便 jQuery 知道它是一个类。

关于jquery 选择器 : child elements of the same class inside divs of different classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28942517/

相关文章:

jquery - jQuery YouTube检索描述

javascript - 使用 jquery 添加元素后隐藏搜索栏

javascript - 如何在选择的数据加载之前显示进度条?

javascript - 我不知道出了什么问题,当我点击按钮时没有任何作用

css - Compass + Blueprint 非语义类

css - rails : font don't appear in production

javascript - jquery $.each() 和数组验证

javascript - 使用箭头键滚动 div

html - 使用CSS右对齐按钮

html - 增加R中表格行的高度