Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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
Javascript jQuery中数组中的第一个最近值_Javascript_Jquery - Fatal编程技术网

Javascript jQuery中数组中的第一个最近值

Javascript jQuery中数组中的第一个最近值,javascript,jquery,Javascript,Jquery,HTML代码 <a class="ytclink" href="http://www.youtube.com/watch?v=wB0JY_F3370"> <img width="100" height="100" src="http://img.youtube.com/vi/0GU-sgt3irU/0.jpg"> </a> <h5 class="ytctitle"> <a class="ytclink" href="http:/

HTML代码

<a class="ytclink" href="http://www.youtube.com/watch?v=wB0JY_F3370">
    <img width="100" height="100" src="http://img.youtube.com/vi/0GU-sgt3irU/0.jpg">
</a>
<h5 class="ytctitle">
  <a class="ytclink"  href="http://www.youtube.com/watch?v=wB0JY_F3370">Play</a>
</h5>
  <a class="ytclink" href="http://www.youtube.com/watch?v=hB0er_F3356">
    <img width="100" height="100" src="http://img.youtube.com/vi/0GU-sgt3irU/0.jpg">
  </a>
<h5 class="ytctitle">
  <a class="ytclink"  href="http://www.youtube.com/watch?v=hB0er_F3356">Play</a>
</h5>

<a class="ytclink" href="http://www.youtube.com/watch?v=ghhh_F6667">
   <img width="100" height="100" src="http://img.youtube.com/vi/0GU-sgt3irU/0.jpg">
</a>
<h5 class="ytctitle">
  <a class="ytclink"  href="http://www.youtube.com/watch?v=ghhh_F6667">Play</a>
</h5>
到目前为止,我的代码运行良好,但如果我单击任何a标记、h5标记,则需要进行一些更改。因此,请从页面开始推入href值,如果单击下面的代码,则希望推入第一个最接近的href值

<a class="ytclink" href="http://www.youtube.com/watch?v=hB0er_F3356"><img src="video.jgp"</a>
<h5 class="ytctitle"><a class="ytclink"  href="http://www.youtube.com/watch?v=hB0er_F3356">Play</a></h5>

下面的代码对我来说很好

var utubes= [];
$('.ytclink').click(function(){

       var index1 =  $('.ytclink').index(this);
    index1=parseInt(index1/2);

        var length  = $('.parentDiv  .ytclink').size()/2;
    var i=1;

    while ( index1-i >-1 || index1+i<length){
        if(index1+i<length)
        utubes.push($('.parentDiv > .ytclink').eq(index1 +i).attr('href').split("watch?v=")[1]);
        if(index1-i >-1)
        utubes.push($('.parentDiv > .ytclink').eq( index1 -i ).attr('href').split("watch?v=")[1]);
        i++;
    }

    utubes.push($(this).attr('href').split("watch?v=")[1]);
        alert(utubes);
    });
var-utubes=[];
$('.ytclink')。单击(函数(){
var index1=$('.ytclink').index(本);
index1=parseInt(index1/2);
变量长度=$('.parentDiv.ytclink').size()/2;
var i=1;
而(index1-i>-1 | | index1+i-1)
utubes.push($('.parentDiv>.ytclink').eq(index1-i).attr('href').split(“watch?v=“)[1]);
i++;
}
utubes.push($(this.attr('href').split(“watch?v=“)[1]);
警报(utubes);
});

在本例中,您希望alert仅显示hB0er_F3356吗?如果我单击第二个a或h5标记需要输出,如ghhh_F6667、wB0JY_F3370、hB0er_F3356,如果单击第三个a或h5标记需要输出,如wB0JY_F3370、hB0er_F3356、ghhh_F6667,则使用数字而不是ID来表示您想要的内容可能会有所帮助。例如,1,2,3。试试这把小提琴
   ghhh_F6667,wB0JY_F3370,hB0er_F3356
var utubes= [];
$('.ytclink').click(function(){

       var index1 =  $('.ytclink').index(this);
    index1=parseInt(index1/2);

        var length  = $('.parentDiv  .ytclink').size()/2;
    var i=1;

    while ( index1-i >-1 || index1+i<length){
        if(index1+i<length)
        utubes.push($('.parentDiv > .ytclink').eq(index1 +i).attr('href').split("watch?v=")[1]);
        if(index1-i >-1)
        utubes.push($('.parentDiv > .ytclink').eq( index1 -i ).attr('href').split("watch?v=")[1]);
        i++;
    }

    utubes.push($(this).attr('href').split("watch?v=")[1]);
        alert(utubes);
    });