【问题标题】:jquery selector: child elements of the same class inside divs of different classesjquery选择器:不同类的div中同一类的子元素
【发布时间】:2015-03-09 21:12:15
【问题描述】:

有两个具有“.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 元素?

【问题讨论】:

  • 第一类选择器中缺少句点字符 ("div" + selectedPopup + " ")。

标签: jquery html css jquery-selectors


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2017-05-19
    • 2012-06-13
    • 2010-12-04
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多