admin管理员组文章数量:1401384
I use this jQuery plugin to load SVG image into a div. I'd like to get all the element IDs from the SVG XML data which contain an ID attribute. For example, in my SVG image I have circle
and polygon
elements with ID attributes, but a rect
element without ID attribute. I want to get these two with IDs from the XML.
How do I do this?
I have tried this in the onLoad
method of svg
function.
$("#image svg").each(function() {
$("#list").append("<option>" + this.id + "</option>");
});
#image
is the container for the svg
element and #list
is the select
tag.
I use this jQuery plugin to load SVG image into a div. I'd like to get all the element IDs from the SVG XML data which contain an ID attribute. For example, in my SVG image I have circle
and polygon
elements with ID attributes, but a rect
element without ID attribute. I want to get these two with IDs from the XML.
How do I do this?
I have tried this in the onLoad
method of svg
function.
$("#image svg").each(function() {
$("#list").append("<option>" + this.id + "</option>");
});
#image
is the container for the svg
element and #list
is the select
tag.
- 1 Please post what you have tried so far. We are happy to help you with the code you have. – Felix Kling Commented Nov 19, 2013 at 17:18
- @FelixKling I added the code I've tried. – MikkoP Commented Nov 19, 2013 at 17:24
2 Answers
Reset to default 3Assuming the SVG is added to the page properly, you can use the "has attribute selector" to select all elements with an ID:
$("#imag > svg [id]").each(function() {
$("#list").append("<option>" + this.id + "</option>");
});
Are you sure it's not working?
I've created a fiddle with your code
$("#image svg").each(function() {
$("#list").append("<option>" + this.id + "</option>");
});
and it looks fine to me.
Edit: Replace the second line with this:
$("#list").append("<option style='display:" + ((this.id.length>0)?'inline':'none') + ";'>" + this.id + "</option>");
to show only the ones with the ID. Or just put a check(length>0, or something) before appending the option tag(which would probably be easier to understand/modify later on, I just like to use ternary operators :p )
本文标签: javascriptjQuery get all elements inside SVG with ID specifiedStack Overflow
版权声明:本文标题:javascript - jQuery get all elements inside SVG with ID specified - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744231135a2596340.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论