使用 jquery 选择器获取 iframe 内容
有没有办法通过选择器访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我曾经遇到过这个问题,我觉得它很乏味。我从未找到如何编写这样的单个选择器的解决方案。
即便如此,选择器仍然相当长。对我来说最明显的解决方案是将其保存到变量中。
这样更好吗?
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.
Is that better?
直观上,将所有内容打包到一个选择器中似乎更优雅,但事实是,即使有这样一个选择器,从性能角度来看,使用 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.
添加到这里供后代使用。我最终采用的解决方案是使用一些自定义解析代码覆盖根 jquery 对象。像这样的事情:
请注意,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:
Note that double colon (
::
) in css means select pseudo element, while single colon means select by pseudo class.您可以创建自己的自定义选择器。喜欢:
用法应该像
You can create your own custom selector. Like:
Usage should be like