如何从iframe获取href链接 [英] How to get the href link from iframe

查看:462
本文介绍了如何从iframe获取href链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      my html content is in iframe.
        <iframe id="test" name=""src="test.html">
        .....content from test.html

      </iframe>

test.html(在我的服务器中)具有一个类似

test.html(in my server) has a link like

         <div id="division"><a  href="www.google.com"></a></div>

jquery

              $(document).ready(function() {
           var value=$("#division").find('a').attr('href');
        alert(value);
                }

question:alert(value)返回未定义

question:alert(value) returns undefined

推荐答案

一旦iframe加载完毕,您就必须使用contents()获取内容,然后搜索并获取锚点的href属性,并且加载的内容不能跨域,具有相同的来源策略等:

Once the iframe has loaded, you'll have to use contents() to get the content, and then search and get the href property of the anchor, and the loaded content can't be cross domain, same origin policy etc :

$(document).ready(function() {
    $('#test').on('load', function() {
        var value = $(this).contents().find("#division a").prop('href');
        alert(value);
    });
});

这篇关于如何从iframe获取href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆