简体   繁体   English

在锚点内选择div

[英]Selecting div within anchor

How to select div tag inside an anchor tag. 如何在锚标签内选择div标签。 Consider the following HTML. 考虑以下HTML。

<a target="_new" href="https://stackoverflow.com/12.mp4"
       data-placement="top" rel="tooltip" 
       title="Video (MP4)" >
       <div class="hidden">Interesting 12</div>
    </a>

I have to select all anchor tags pointing to video resource. 我必须选择所有指向视频资源的锚标签。 I am able to select the anchor tags. 我可以选择锚标签。 Now How do I select the division inside it and get the text? 现在如何选择其中的分隔并获取文本?

$("a").each(function() {
    var link = $(this).attr('href');
    if (typeof link == "string" && link.indexOf(".mp4") > 0) {
        alert(link);
    }
})​
$('a[href*=".mp4"]').each(function() {
   alert( this.href );
   $(this).find('div.hidden').text();
});

OR 要么

$("a").each(function() {
    var link = $(this).attr('href');
    if (typeof link == "string" && link.indexOf(".mp4") > 0) {
        alert(link);

        $(this).find('div.hidden').text();
        // OR
        $('div.hidden', this).text();
    }
})​;

NOTE 注意

Placing a div within anchor tag is not valid. div放在锚标记中无效。

use .find('div') or .find('.hidden') after your $('a') $('a')之后使用.find('div').find('.hidden') $('a')

http://api.jquery.com/find/ http://api.jquery.com/find/

$("a").each(function(){
    $(this).find('div').each(function(){
        ....
    });
});

use .find() 使用.find()

$("a").each(function() {
    var link = $(this).attr('href');
    if (typeof link == "string" && link.indexOf(".mp4") > 0) {
        alert(link);
    }

    // get the div
    var divText = $(this).find('div').text();

})​
$("a").each(function() {

    var link = $(this).attr('href');

    a=link.length;

    var filetype=link[a-3]+link[a-2]+link[a-1]

    if(link[a-4] == '.' && filetype=='mp4') //you can check more extensions here using ||.
      $(this).find('.hidden').text()          


})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM