Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery选择器未选择值_Jquery - Fatal编程技术网

Jquery选择器未选择值

Jquery选择器未选择值,jquery,Jquery,下面是我的表,我正在尝试从表中选择元素 <table id="DetailsGridViewDiv_Header" style="border: 1px solid;"> <tbody> <tr> <th>A</th> <th>B</th> <th>C</th> <

下面是我的表,我正在尝试从表中选择元素

<table id="DetailsGridViewDiv_Header" style="border: 1px solid;">
    <tbody>
        <tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
            <th>E</th>
            <th>F</th>
            <th>G</th>
            <th>H</th>
        </tr>
    </tbody>
</table>
它不会返回任何非id DetailsGridViewDiv_标题表的直接后代的字母,因此应为:

$("#DetailsGridViewDiv_Header th:nth-child(3)").text()
th不是id为DetailsGridViewDiv_标头的表的直接子代,因此应为:

$("#DetailsGridViewDiv_Header th:nth-child(3)").text()

这个问题是因为您使用的是直接后代选择器>,而th不是DetailsGridViewDiv_头的直接后代。卸下该操作员,它将正常工作:

alert($("#DetailsGridViewDiv_Header th:nth-child(3)").text());
如果要保留子体选择器,则需要按顺序跟随子体元素:

alert($("#DetailsGridViewDiv_Header > tbody > tr > th:nth-child(3)").text());

这个问题是因为您使用的是直接后代选择器>,而th不是DetailsGridViewDiv_头的直接后代。卸下该操作员,它将正常工作:

alert($("#DetailsGridViewDiv_Header th:nth-child(3)").text());
如果要保留子体选择器,则需要按顺序跟随子体元素:

alert($("#DetailsGridViewDiv_Header > tbody > tr > th:nth-child(3)").text());

当您在jQuery中选择某个对象时,它将返回一个数组,因此使用eq函数选择jQuery对象可能更有意义。谢谢,Rory!:

var $thArray = $("#DetailsGridViewDiv_Header").find("th");
alert($thArray.eq(2).text());

当您在jQuery中选择某个对象时,它将返回一个数组,因此使用eq函数选择jQuery对象可能更有意义。谢谢,Rory!:

var $thArray = $("#DetailsGridViewDiv_Header").find("th");
alert($thArray.eq(2).text());

哈哈,@Rory,第一次编辑是什么D@nicael妈的,我以为我侥幸逃脱了:d我在剪贴板上编辑了另一个问题,不小心把它粘贴到了L里,@Rory,第一次编辑是什么:D@nicael该死我想我已经逃脱了:d我在剪贴板中编辑了另一个问题,不小心粘贴了它,这会给你一个错误-你调用的是DomeElement上的文本,而不是jQuery对象。不,find返回一个jQuery对象。没错,但是通过索引$thArray[2]访问它会返回一个DomeElement。没问题。仅供参考,eq可能是一种更好的方法,因为它会让您返回一个jQuery对象。请注意,这会给您一个错误-您调用的是DomeElement上的文本,而不是jQuery对象。不,find返回一个jQuery对象。没错,但通过索引$thArray[2]访问它会返回一个DomeElement。没问题。仅供参考,eq可能是一种更好的方法,因为它将使您返回jQuery对象。