使用 jquery 选择器获取 iframe 内容

发布于 2024-11-06 15:43:17 字数 248 浏览 5 评论 0原文

有没有办法通过选择器访问 iframe 的内容?像这样的事情:

$("iframe::contents .my-foo")

我不断访问我当前正在处理的项目的 iframe 内容,并且 $("iframe").contents().find(".my-foo") 是打字变得有点乏味。

如果 jquery 中不存在此功能,是否有提供此功能的插件?如果不是我怎么写这样的插件?

Is there anyway to access an iframe's contents via a selector? Something like this:

$("iframe::contents .my-foo")

I'm constantly accessing an iframe's contents for a project I'm currently working on and $("iframe").contents().find(".my-foo") is becoming a bit tedious to type out.

If this feature doesn't exist in jquery out of the box, is there a plugin that provides this functionality? If not how could I write such a plugin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-11-13 15:43:17

我曾经遇到过这个问题,我觉得它很乏味。我从未找到如何编写这样的单个选择器的解决方案。

即便如此,选择器仍然相当长。对我来说最明显的解决方案是将其保存到变量中。

var frame = $("iframe").contents();

frame.find(".my-foo")
...

这样更好吗?

I had this issue once where I found it tedious. I never found a solution for how to write a single selector like that.

Even so, the selector is still rather long. The most obvious solution to me is just save it to a variable.

var frame = $("iframe").contents();

frame.find(".my-foo")
...

Is that better?

但可醉心 2024-11-13 15:43:17

直观上,将所有内容打包到一个选择器中似乎更优雅,但事实是,即使有这样一个选择器,从性能角度来看,使用 find() 遍历会更好。那么 jQuery 就不必解析和分析字符串了。

Intuitively, it seems more elegant to pack everything into the one selector, but the truth is that, even if there were such a selector, it is better from a performance perspective to traverse with find(). Then jQuery doesn't have to parse and analyze the string.

夏日落 2024-11-13 15:43:17

添加到这里供后代使用。我最终采用的解决方案是使用一些自定义解析代码覆盖根 jquery 对象。像这样的事情:

(function() {
    var rootjq = window.jQuery;

    var myjq = function(selector, context) {
        if(selector.indexOf("::contents") === -1) {
            return rootjq(selector, context);
        } else {
            var split = selector.split("::contents");

            var ifrm = split[0];
            var subsel = split.splice(1).join("::contents");

            var contents = rootjq(ifrm, context).contents();

            // Recursive call to support multiple ::contents in a query
            return myjq(subsel, contents);
        }
    };
    myjq.prototype = myjq.fn = rootjq.fn;

    window.jQuery = window.$ = myjq;
})();

请注意,css中的双冒号(::)表示选择伪元素,而单冒号表示冒号表示按伪类选择。

Added here for posterity. The solution I ended up going with was to override the root jquery object with a bit of custom parsing code. Something like this:

(function() {
    var rootjq = window.jQuery;

    var myjq = function(selector, context) {
        if(selector.indexOf("::contents") === -1) {
            return rootjq(selector, context);
        } else {
            var split = selector.split("::contents");

            var ifrm = split[0];
            var subsel = split.splice(1).join("::contents");

            var contents = rootjq(ifrm, context).contents();

            // Recursive call to support multiple ::contents in a query
            return myjq(subsel, contents);
        }
    };
    myjq.prototype = myjq.fn = rootjq.fn;

    window.jQuery = window.$ = myjq;
})();

Note that double colon (::) in css means select pseudo element, while single colon means select by pseudo class.

甩你一脸翔 2024-11-13 15:43:17

您可以创建自己的自定义选择器。喜欢:

$.extend($.expr[':'], {
    contents: function(elem, i, attr){
      return $(elem).contents().find( attr[3] );
    }
});  

用法应该像

$('iframe:contents(.my-foo)').remove(); 

You can create your own custom selector. Like:

$.extend($.expr[':'], {
    contents: function(elem, i, attr){
      return $(elem).contents().find( attr[3] );
    }
});  

Usage should be like

$('iframe:contents(.my-foo)').remove(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文