jquery 选择器:不同 class 的 div 中相同 class 的子元素
jquery selector: child elements of the same class inside divs of different classes
有两个 div,class 是“.div1”和“.div2”。
它们每个都包含相同的代码 - 具有相同 class 名称的相同元素。
如果我需要 set/get 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'... 不希望使用 itsinnerelementclass 元素位于具有 class of nodeDiv1class 的元素内部某处?
我认为您需要在 hostPopup
的开头添加一个点,以便 jQuery 知道它是 class。
有两个 div,class 是“.div1”和“.div2”。 它们每个都包含相同的代码 - 具有相同 class 名称的相同元素。 如果我需要 set/get 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'... 不希望使用 itsinnerelementclass 元素位于具有 class of nodeDiv1class 的元素内部某处?
我认为您需要在 hostPopup
的开头添加一个点,以便 jQuery 知道它是 class。