有两个类为“.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/