簡體   English   中英

在錨點內選擇div

[英]Selecting div within anchor

如何在錨標簽內選擇div標簽。 考慮以下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>

我必須選擇所有指向視頻資源的錨標簽。 我可以選擇錨標簽。 現在如何選擇其中的分隔並獲取文本?

$("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();
});

要么

$("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();
    }
})​;

注意

div放在錨標記中無效。

$('a')之后使用.find('div').find('.hidden') $('a')

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

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

使用.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