javascript jQuery 中的内容文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5990349/
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
contentDocument in jQuery
提问by user752135
I have the following js script to access elements inside a object (SVG - <object data="bbbbbb.svg" type="image/svg+xml" id="alphasvg" width="100%" height="100%"></object>
)
我有以下 js 脚本来访问对象内的元素(SVG - <object data="bbbbbb.svg" type="image/svg+xml" id="alphasvg" width="100%" height="100%"></object>
)
jQuery(document).ready(function($) {
$(window).load(function () {
var a = document.getElementById("alphasvg");
var svgDoc = a.contentDocument;
var delta = svgDoc.getElementsByTagName("path");
$(delta).click(function() {
//do stuff
})
});
});
I want to use jQuery to access the elements and tags. I'm completely stuck on the contentDocument part. How can I convert this to jQuery so I can use attr etc?
我想使用 jQuery 来访问元素和标签。我完全停留在 contentDocument 部分。如何将其转换为 jQuery 以便我可以使用 attr 等?
I want to be able to access and modify attributes in jQuery instead of having to use the traditional js methods which I am unfamiliar with.
我希望能够访问和修改 jQuery 中的属性,而不必使用我不熟悉的传统 js 方法。
How someone can help me?
有人可以如何帮助我?
Thanks a million.
太感谢了。
回答by Gary Green
You should be able to access the paths directly like elements, no need for contentDocument or getElementsByTagName, etc:
您应该能够像元素一样直接访问路径,不需要 contentDocument 或 getElementsByTagName 等:
jQuery(document).ready(function($) {
$(window).load(function () {
$("#alphasvg path").click(function() {
//do stuff
// $(this).attr('d') = the path
})
});
});
回答by Salman A
Like this:
像这样:
$(svgDoc).find("whatever").doWhatever();
Demo hereand code here. Note that I've used an <iframe>
for demonstration hence the first URL will work, second will give you "permission denied" error if you try to runthe fiddle.
在此处演示并在此处编写代码。请注意,我使用了一个<iframe>
用于演示,因此第一个 URL 将起作用,如果您尝试运行该 fiddle ,第二个 URL 会给您“权限被拒绝”错误。
回答by jbeard4
If you are embedding SVG into HTML using the object tag (as opposed to inline SVG), this then this is a duplicate of a previous question, the answer to which may be found here: How to access SVG elements with Javascript
如果您使用对象标签(而不是内联 SVG)将 SVG 嵌入到 HTML 中,那么这是上一个问题的重复,可以在此处找到答案:如何使用 Javascript 访问 SVG 元素