jQuery 从唯一 div 内的 iframe 获取“src”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16500843/
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-26 17:15:11  来源:igfitidea点击:

Get "src" from an iframe inside a unique div

jqueryfor-loop

提问by Kim

How is it possible here to get the "src" from the iframe that is inside the div "youtubeHolder"? What's happening in the code below is that it search for all id's starting with youtubeHolder. And there are multiple holders, that is dynamically created as youtubeHolder1, youtubeHolder2 etc.

这里怎么可能从div“youtubeHolder”内的iframe中获取“src”?下面代码中发生的事情是它搜索所有以 youtubeHolder 开头的 id。并且有多个持有者,动态创建为 youtubeHolder1、youtubeHolder2 等。

Any ideas?

有任何想法吗?

Jquery:

查询:

$(document).ready(function() {
$('a#export').on('click',function(){
    var contentMap = {};
    $('[id^="youtubeHolder"]').each(function(){
        contentMap[this.id] = $(this).html();
        });
        for(id in contentMap) {

$.ajax({
    url: "post.php",
    type: "post",
    data: { 
        ExportDivID: id,
        ExportDivContent: contentMap[id]
    },
    success: function(){
        alert("success");
    },
    error:function(){
        alert("failure");
    }   
    }); 
    }
    });
    });

Many thanks in advance!

提前谢谢了!

回答by Felix G.

You can get an attribute using .attr('src')from jQuery

您可以使用.attr('src')jQuery获取属性

More information about that you can find at the jQuery API

有关更多信息,您可以在jQuery API 中找到

So you can get the src-attribute in you code like this:

所以你可以在你的代码中获得 src-attribute,如下所示:

$(document).ready(function() {
$('a#export').on('click',function(){
    var contentMap = {};
    $('[id^="youtubeHolder"]').each(function(){
        contentMap[this.id] = $(this).html();
        var src = $(this).attr('src');
        alert(src);
        });

    });
    });

I stripped it a little bit, sorry. The most important line is the line var src = $(this).attr('src')

我把它剥了一点,对不起。最重要的线是线var src = $(this).attr('src')

See a working example at http://jsfiddle.net/zP3ED/. It outputs the src-attribute as JavaScript-alarm. You can use the JavaScript variable srcafter that.

请参阅http://jsfiddle.net/zP3ED/ 上的一个工作示例。它将 src-attribute 输出为 JavaScript-alarm。之后您可以使用 JavaScript 变量src

I hope this helps.

我希望这有帮助。

Update:

更新:

If you want to use something like this:

如果你想使用这样的东西:

<div id="youtubeHolder">
    <iframe src="http://youtube.com/test"></iframe>
</div>

You can do this with a multiple selector like this:

你可以用这样的多重选择器来做到这一点:

var src = $('iframe',this).attr('src');

See an working example here: http://jsfiddle.net/TIIUNDER/EPjQC/

在此处查看一个工作示例:http: //jsfiddle.net/TIIUNDER/EPjQC/