Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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显示错误的p索引_Jquery - Fatal编程技术网

jQuery显示错误的p索引

jQuery显示错误的p索引,jquery,Jquery,所以我在这里举了一个小例子: 这表明p元素有2个索引,而不是0(然后悬停在第一个p元素上)。为什么会这样 $('body').on('hover', '.trn p', function() { var ind = $(this).index(); var ps = $('.trn:visible p:eq(' + ind + ')').html(); console.log(ps); console.log(ind); }); 您的结构如下所示: <div class="trn"&g

所以我在这里举了一个小例子:

这表明p元素有2个索引,而不是0(然后悬停在第一个p元素上)。为什么会这样

$('body').on('hover', '.trn p', function() {
var ind = $(this).index();
var ps = $('.trn:visible p:eq(' + ind + ')').html();
console.log(ps);
console.log(ind);
});

您的结构如下所示:

<div class="trn">
    <img />
    <h1></h1> 
    <p></p>
    <p></p>
</div>
您可以使用
$(this)
$(event.target)
,其结果相同:

$('body').on('hover', '.trn p', function(event) {
    var ps1 = $(this).html(),
        ps2 = $(event.target).html();
    console.log(ps1);
    console.log(ps2);
});

如果我想获得父元素中p元素的索引,排除所有其他元素,该怎么办?然后使用
varind=$('.trnp').index($(this))
$('body').on('hover', '.trn p', function(event) {
    var ps1 = $(this).html(),
        ps2 = $(event.target).html();
    console.log(ps1);
    console.log(ps2);
});