Jquery next()不在span上工作

时间:2017-07-26 20:26:00

标签: jquery

我的html看起来像这样:

link_to

我想抓住"一般责任,工人补偿,"作为一个字符串。

使用jQuery,我可以通过

获得正确的subHeader
<span class="subHeader">Line of Business: </span>
<br>
<span class="sk-hits-grid-hit__state" style="margin: 5px; text-align: center;">General Liability,Workers Comp,</span>

(我有其他subHeaders所以我必须检查文本)。我想抓住兄弟跨度元素。我已经尝试了sibling()和next(),它们都没有返回任何东西。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

它是.siblings(),这将得到你提到的范围

$('#subHeader').siblings('span.sk-hits-grid-hit__state').text()

例如:

&#13;
&#13;
console.log($('#subHeader').siblings('span.sk-hits-grid-hit__state').text())
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="subHeader">Line of Business: </span>
<br>
<span class="sk-hits-grid-hit__state" style="margin: 5px; text-align: center;">General Liability,Workers Comp,</span>
&#13;
&#13;
&#13;

$('#subHeader').next().next().text()(由<br>引起的两个联接)

&#13;
&#13;
console.log($('#subHeader').next().next().text())
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="subHeader">Line of Business: </span>
<br>
<span class="sk-hits-grid-hit__state" style="margin: 5px; text-align: center;">General Liability,Workers Comp,</span>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我们可以通过将.filter()方法链接到.siblings()方法来利用span.subHeader方法。此代码假定每个span.sk-hits-grid-hit__state<span class="subHeader">Line of Business: </span> <br> <span class="sk-hits-grid-hit__state" style="margin: 5px; text-align: center;">General Liability,Workers Comp,</span>

之间存在一对一的比率
  

<强> HTML

.subHeader {
  font-weight: bold;
  font-size: .83em;
  font-family: arial;
}

.turn_blue {
  color: blue;
  font-size: .83em;
  font-family: arial
}
  

<强> CSS

var sub_headers_array = $('.subHeader').filter(function() {
    return $(this).text() === "Line of Business: ";
});

sub_headers_array.each(function(index, current_cell) {
    var business_text_span = $(current_cell).siblings().filter(function() {
        return $(this).text() === "General Liability,Workers Comp,";
    });

    business_text_span.addClass('turn_blue')

    console.log(business_text_span);
});
  

<强>的JavaScript / jQuery的

var sub_headers_array = $('.subHeader').filter(function() {
  return $(this).text() === "Line of Business: ";
});

sub_headers_array.each(function(index, current_cell) {
  var business_text_span = $(current_cell).siblings().filter(function() {
    return $(this).text() === "General Liability,Workers Comp,";
  });

  business_text_span.addClass('turn_blue');

  console.log(business_text_span);
});

.subHeader {
  font-weight: bold;
  font-size: .83em;
  font-family: arial;
}

.turn_blue {
  color: blue;
  font-size: .83em;
  font-family: arial
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span class="subHeader">Line of Business: </span>
<br>
<span class="sk-hits-grid-hit__state" style="margin: 5px; text-align: center;">General Liability,Workers Comp,</span>
import QtQuick 2.0

Rectangle {
    id: myRect
    width: 100; height: 100
    color: "black"

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: myRect.state == 'clicked' ? myRect.state = "" : myRect.state = 'clicked';
    }

    states: [
        State {
            name: "clicked"
            PropertyChanges { target: myRect; color: "red" }
        }
    ]
}