我有以下代码所有三个DIV都有相同的类名我想要做的是通过使用jquery索引号访问/检索第二个DIV值第二个DIV的值是3.21,我试图使用这个代码来检索public void Play(){
TextWriter tw = new StreamWriter("helloWorld.txt", true);
tw.WriteLine(System.DateTime.Now);
tw.Close();
}
没有运气,不知道我哪里错了?
div.OddVal.index(2)
答案 0 :(得分:5)
您已使用 jQuery 标记了该问题,因此我相信您正在寻找.eq()
:
将匹配元素集合减少到指定索引处的元素。
console.log($('.OddVal').eq(1).text());

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.37</div>
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.21</div>
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">2.07</div>
&#13;
答案 1 :(得分:2)
在其他答案中有多种方式解释eq
,所以我不会添加它。
我使用了n:th
选择器和each
循环
$(".OddVal").each(function(index) {
if (index === 1) {
console.log("Each loop result " + $(this).text());
}
});
console.log("nth slector result " + $(".OddVal:nth-child(2)").text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ThreeWayCol OddsCol1">
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.37</div>
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">3.21</div>
<div class="OddVal ng-pristine ng-untouched ng-valid ng-binding ng-scope ng-not-empty" ng-repeat="sel in event._matchMarket.selections" ng-model="sel" ng-class="{'Sel': oddSelections[event.id].sels[sel.id]}" ng-click="selectOdd(event,event._matchMarket,sel)">2.07</div>
</div>
答案 2 :(得分:1)
使用jquery eq()
方法将指定索引的值作为
var val = $('.OddVal').eq(your_index_no_here).text();// must be numeric as 1,2,3
alert(val);
您可以获取文档JQuery eq()
答案 3 :(得分:1)
答案 4 :(得分:0)
您可以使用https://api.jquery.com/get/
.get()方法授予对每个jQuery对象下面的DOM节点的访问权限。如果index的值超出范围 - 小于元素的负数或等于或大于元素的数量 - 则返回undefined。考虑一个简单的无序列表:
<ul>
<li id="foo">foo</li>
<li id="bar">bar</li>
</ul>
指定索引后,.get(index)将检索单个元素:
console.log( $( "li" ).get( 0 ) );
由于索引从零开始,因此返回第一个列表项:
<li id="foo">