CSS访问输入上的顶部标签元素

时间:2016-12-19 12:06:55

标签: java html css

当我遇到验证问题时,我正在尝试设置标签样式以更改其颜色。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Collect the nav links, forms, and other content for toggling -->
<nav class="navbar navbar-default navbar-fixed-top" id="navtop">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar top-bar"></span>
        <span class="icon-bar middle-bar"></span>
        <span class="icon-bar bottom-bar"></span>
      </button>
      <a class="navbar-brand" id="topper" href="#">
        <p>&lt;&frasl;BY&#95;SUONPERA&gt;</p>
      </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#About">About</a>
        </li>
        <li><a href="#My_Work">My Work</a>
        </li>
        <li><a href="#Contact">Contact</a>
        </li>
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

<!-- Header -->
<div class="header">
  <div class="container" style="display:flex;justify-content:center;align-items:center;">
    <div id="rowheader">
      <h1>HI</h1>
      <h2>blablablablar</h2>
    </div>
  </div>
</div>

<!-- Section1 -->
<div id="section1" class="container">
  <h2 id="About">About</h2>
  <div class="row">
    <div class="col-md-6 col-md-offset-3">
      <h3>Me</h3>
      <p>blablablablabla</p>
      <br>
      <br>
    </div>
  </div>
</div>



<!-- Section2 -->
<div id="section2" class="container">
  <h2 id="My_Work">My Work</h2>
</div>

<!-- Section3 -->
<div id="section3" style="display:flex;justify-content:center;align-items:center;">
  <div>
    <h2 id="Contact">Want to get in touch?</h2>	
    <div class="container">
      <div class="row">
        <div class="col-md-6 col-md-offset-3">
          <p>blablablabla</p>
          <a href="#"><i class="fa fa-envelope fa-2x" aria-hidden="true"></i></a>	
          <a href="#"><i class="fa fa-linkedin-square fa-2x" aria-hidden="true"></i			></a>	
        </div>
      </div>
    </div>
  </div>
</div>


<!-- Footer -->
<div id="footer">
  <a id="topper" href="#navtop">BACK TO TOP</a>
  <p>Copyright &copy; 2016</p>
</div>
</body>

</html>

大部分信息都是动态生成的。我的目的是访问顶部,以便我可以更改“选项2”文本的颜色。

我目前有以下内容:

<label>
<input id="single-select" class="apply-form-multi-select required errorField" type="radio" name="question_3897788" style="background-color: rgb(0, 255, 0); color: red;">
Option 2
</label>

但这只给了我输入字段。 关于如何访问标签的任何想法?

1 个答案:

答案 0 :(得分:2)

您可以尝试将.parent()附加到匹配标记正上方标记的末尾。

$('input[name='+i+'].errorField.apply-form-multi-select').parent().css("color", "red");

感谢Amit kumar,这是一个确保父元素是标签

的解决方案

如果要确保.parent()label元素匹配,可以为其指定选择器作为参数。例如,.parent("label")