如何使用jquery在iframe中定位项目 [英] How to target an item in an iframe with jquery

查看:269
本文介绍了如何使用jquery在iframe中定位项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我网站上的iframe目标和项目中更改内联值?

How do I target and item in an iframe thats on my site and change the inline value?

<iframe class="preview_iframe">
<div id="example">
<a id="ex">
<svg fill="#000000"></svg>
</a>
</div>
</iframe>

我需要在iframe中定位SVG(多个)并使用jquery更改填充(颜色) ,我怎么能这样做,我试过,但它似乎没有选择它。

I need to target an SVG (multiple) in an iframe and change the fill (color) with jquery, how can I do this I tried but it doesn't seem to select it.

继承人试试的jquery

Heres the jquery im trying


         $("[id^=like_iframe_]").load(function() {
            var frame = $("[id^=like_iframe_]").contents();
            $("svg", frame).css({'fill' : 'red'});
           });


$("[id^=like_iframe_]").contents().find("svg").css({'fill' : 'red'});

选择框架,但不改变svg的颜色

Its selecting the frame, but it's not changing the color of the svg

推荐答案

我发现以下内容(见下文)适用于我:

I found that the following (see below) works for me:

<html><head>
<script type="text/javascript" src="Downloads/jquery-1.10.2.min.js"></script>
<script type="text/javascript">function tryit(){
  $("iframe[id$=iframe]").contents().find("ellipse").css({fill:"red"});
}</script>
</head><body>
<a href="#" onclick='tryit()'>click here</a><br/>
<iframe id="preview_iframe" src="stackoverflow19846765_iframe.html">
This text won't show if browser supports IFRAMEs(?).</iframe>
</body></html>

文件stackoverflow19846765_iframe.html读取:

File stackoverflow19846765_iframe.html reads:

<html><head></head><body>
<div id="example"> <a id="ex" onclick="alert('clicked svg')">
<svg fill="#000000"> 
  <ellipse cx="110" cy="60" rx="100" ry="50" style="fill:blue" />
</svg>
</a> </div>
</body></html>

我必须使用 src 属性 iframe (并将源放在单独的文件中)。 (否则 iframe 显示为空。)

I had to use a src attribute for the iframe (and put the source in a separate file). (Otherwise the iframe appeared empty.)

id 似乎与代码中的HTML片段(使用 class )不匹配(?)。我使用 iframe id = 并使用 id * = iframe 选择它。

The selector on id does not seem to match(?) the HTML fragment (using class) in your code. I used iframe id= and selected it with id*=iframe.

我必须在 svg 中放置一些内容才能看到发生的任何事情。

I had to put something within the svg to see anything happen.

注:尝试时使用本地文件的代码,只有Firefox让我查看 iframe 。 Chrome会因本地文件的原始政策而阻止此操作。

NOTE: When trying the code with local files, only Firefox lets me look into the iframe. Chrome prevents that due to same origin policy on local files.

这篇关于如何使用jquery在iframe中定位项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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