jquery添加类以选择所选元素

时间:2018-02-23 13:16:30

标签: javascript jquery html css

我想用一个按钮点击更改段落的颜色。在我的代码中,我有3个段落和3个按钮。我的代码运行正常。但是当我点击一个按钮时,它也会改变其他段落的颜色。我只想改变包含按钮的段落的颜色。我想通过按钮将该类仅添加到该段落部分。



contours =  cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
ctr = numpy.array(contours).reshape((-1,1,2)).astype(numpy.int32)
cv2.drawContours(img, ctr, -1, (0, 255, 0), 3)

Option explicit             ' declaration head of code module

Sub LoadIt()
Dim xmlCurvesFilePath        As String
    xmlCurvesFilePath = ThisWorkbook.Path & "\xml\testxc.xml"  ' << change to your xml file

Dim xmlCurvesFileDOMDocument As New DOMDocument60
Dim listOfMaturities         As IXMLDOMNodeList
Dim XmlNamespaces            As String
   ' [*** define Namespaces ***] ' <<< NAMESPACE DEFINITION NEEDED :-)
    XmlNamespaces = "xmlns:xc='XmlCache' xmlns:mp='mx.MarketParameters' xmlns:rt='mx.MarketParameters.Rates' xmlns:rtcu='mx.MarketParameters.Rates.Curve'"

With xmlCurvesFileDOMDocument
    .setProperty "SelectionNamespaces", XmlNamespaces
    .setProperty "SelectionLanguage", "XPath"
    .resolveExternals = True
    .validateOnParse = True
    .async = False
    If .Load(xmlCurvesFilePath) Then   ' check correct loading
       Set listOfMaturities = .SelectNodes("//xc:XmlCache/xc:XmlCacheArea/mp:nickName/mp:date/rt:rate/rtcu:curve/rtcu:currency/rtcu:label/rtcu:type/rtcu:generator/rtcu:market/rtcu:maturity")
       Debug.Print listOfMaturities.Length
    Else
       Dim xPE        As Object    ' Set xPE = CreateObject("MSXML2.IXMLDOMParseError")
       Dim strErrText As String
       Set xPE = xmlCurvesFileDOMDocument.parseError
       With xPE
           strErrText = "Load error " & .ErrorCode & " xml file " & vbCrLf & _
          Replace(.URL, "file:///", "") & vbCrLf & vbCrLf & _
           xPE.reason & _
          "Source Text: " & .srcText & vbCrLf & vbCrLf & _
          "Line No.:    " & .Line & vbCrLf & _
          "Line Pos.: " & .linepos & vbCrLf & _
          "File Pos.:  " & .filepos & vbCrLf & vbCrLf
      End With
      MsgBox strErrText, vbExclamation
      Set xPE = Nothing
      Exit Sub
    End If
End With

' further code
' ...
' clear memory
Set xmlCurvesFileDOMDocument = Nothing
End Sub
&#13;
    function changecolor() {
        $('.section1').addClass('color');
    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

而不是函数使用事件,您可以使用this轻松处理它,而无需将函数添加到html中的每个按钮。

&#13;
&#13;
$('.btn').click(function() {
  $(this).parent().addClass('color');
})
&#13;
.section1 {
  width: 50%;
  background-color: #ccc;
  padding: 10px 0;
  margin: 10px;
}

.btn {
  background-color: red;
  padding: 10px;
  border: 1px solid #ccc;
}

.section1.color {
  background-color: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
  <div class="section1">
    <a href="#" class="btn"> Button </a>
    <p class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
  </div>
  <div class="section1">
    <a href="#" class="btn"> Button </a>
    <p class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
  </div>
  <div class="section1">
    <a href="#" class="btn"> Button </a>
    <p class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
  </div>
</div>
&#13;
&#13;
&#13;

如果你想删除前一个元素的颜色,只需这样做:

&#13;
&#13;
$('.btn').click(function() {
  $('.color').removeClass('color');
  $(this).parent().addClass('color');
})
&#13;
.section1 {
  width: 50%;
  background-color: #ccc;
  padding: 10px 0;
  margin: 10px;
}

.btn {
  background-color: red;
  padding: 10px;
  border: 1px solid #ccc;
}

.section1.color {
  background-color: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
  <div class="section1">
    <a href="#" class="btn"> Button </a>
    <p class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
  </div>
  <div class="section1">
    <a href="#" class="btn"> Button </a>
    <p class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
  </div>
  <div class="section1">
    <a href="#" class="btn"> Button </a>
    <p class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
  </div>
</div>
&#13;
&#13;
&#13;