Javascript 如何获得第一个内部元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3394780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:27:18  来源:igfitidea点击:

How to get the first inner element?

javascriptjquery

提问by JamesTBennett

So I Want to get the first 'a' tag in this div. This is really driving me nuts. thanks for any help.

所以我想在这个 div 中获得第一个“a”标签。这真的让我发疯。谢谢你的帮助。

<div id="PGD" class="album" onmouseover="load(this)">
     <a class="dl" href="#">DOWNLOAD</a>
</div>

javascript

javascript

function load(dl)
{  

 var ID = $(dl).attr('id');
 var elemnt = $('ID:first').attr('id'); 

} 

回答by Felix Kling

Non-jQuery:(was not tagged with jQuery before, so I included this)

非jQuery的:未标记与jQuery之前,所以我包括在此

  • If you want to get the first child element only:

    var element = document.getElementById('PGD').children[0];
    
  • If you want to get the first anchor element:

    var element = document.getElementById('PGD').getElementsByTagName('a')[0];
    
  • 如果只想获取第一个子元素:

    var element = document.getElementById('PGD').children[0];
    
  • 如果要获取第一个锚元素:

    var element = document.getElementById('PGD').getElementsByTagName('a')[0];
    

With jQuery:

使用 jQuery:

var element = $('#PGD').find('a:first');
// or, to avoid jQuery's pseudo selecors:
// var element = $('#PGD').find('a').first();

and actually your function can just be

实际上你的功能可以是

function load(dl)
{    
   var element = $(dl).find('a:first'); 
} 

Update:

更新:

As you are using jQuery, I suggest to not attach the click handler in your HTML markup. Do it the jQuery way:

当您使用 jQuery 时,我建议不要在 HTML 标记中附加点击处理程序。用 jQuery 的方式来做:

$(function() {
    $("#PGD").mouseover(function() {
         $(this).find('a:first').attr('display','inline');  
        alert($(this).find('a:first').attr('display'));
    });
});

and your HTML:

和你的 HTML:

<div id="PGD" class="album">
     <a class="dl" href="#">DOWNLOAD</a>
</div>

?See for yourself: http://jsfiddle.net/GWgjB/

? 自己看:http: //jsfiddle.net/GWgjB/

回答by ajile

$(ID).find(':first')

See findjQuery command.

请参阅findjQuery 命令。

$('#PGD').find('a:first')

Actualy I've not understanding problem, so I'm trying correct your function, to make it clear for you:

实际上我没有理解问题,所以我正在尝试纠正你的功能,让你清楚:

function load(dl)
{  

// var ID = $(dl).attr('id');
// var elemnt = $('ID:first').attr('id'); // Here is error-mast be like $(ID+':first')

 var ID = $(dl).attr('id');
 var elemnt = $(ID).find('*:first').attr('id'); 

} 

I supose dlthat is $('#PGD'). But child element Ahave not attribute id, what are you trying to find?

我想dl那是$('#PGD')。但是子元素A没有属性id,你想找到什么?

Also See: http://api.jquery.com/category/selectors/

另见:http: //api.jquery.com/category/selectors/

回答by naikus

$("#PGD").children("a:first")

This will give you the first child "a" tag, but not the descendents. E.g.

这会给你第一个孩子“a”标签,但不是后代。例如

     <div id="log">
        <p><a href="foo">Foo</a></p>
        <a href="hello">Hello</a>
        <a href="how">Hello</a>
     </div>

Will give you : <a href="hello">Hello</a>

会给你 : <a href="hello">Hello</a>