简体   繁体   English

jQuery选择里面的SVG锚元素<object>标签

[英]jQuery select SVG anchor elements inside <object> tag

I am having quite a bit of trouble using the anchor elements built into my SVG (displayed as an <object> ).我在使用 SVG 内置的锚元素(显示为<object> )时遇到了很多麻烦。 I am trying to target the anchor elements within my Object with jQuery to toggle its respective piece of content when clicked.我正在尝试使用 jQuery 定位对象中的锚元素,以在单击时切换其各自的内容。

If you take a look at my example, I have 3 links built into my SVG (they are a lighter shade of green than the others).如果您看一下我的示例,我的 SVG 中内置了 3 个链接(它们的绿色比其他链接更浅)。 I used InkScape to create my SVG image, and on the light green areas that are linked are just shapes with the href attribute equal to "#operable-shades", "#gutters-downspouts", and "#gable-pediments" respectively.我使用 InkScape 创建了我的 SVG 图像,并且在链接的浅绿色区域上只是具有 href 属性的形状分别等于“#operable-shades”、“#gutters-downspouts”和“#gable-pediments”。

With my SVG displaying nicely on my page (using an <object> tag), when hovering over the links I have created I get the original object data URL with my specified anchor appended to the end of it (ex. 'https://example.com/wp-content/uploads/GHB_Interface-v0.1.svg#gable-pediments') when I would expect it to be 'https://example.com/#gable-pediments'.随着我的 SVG 在我的页面上很好地显示(使用<object>标签),当将鼠标悬停在我创建的链接上时,我会得到原始对象数据 URL,并在其末尾附加了我指定的锚点(例如“https:// example.com/wp-content/uploads/GHB_Interface-v0.1.svg#gable-pediments') 当我希望它是 'https://example.com/#gable-pediments'。

 <script type="text/javascript"> jQuery(document).ready(function($) { $('.ghb_toggle').hide() $('a[href^="#"]').on('click', function(event) { var target = $(this).attr('href'); $(target).toggle(); }); }); </script>
 .ghb_toggle { display:none; }
 <object type="image/svg+xml" data="https://example.com/wp-content/uploads/test.svg" width="500" height="400"></object> <div id="gable-pediments" class="ghb_toggle"> <p>Content 1</p> </div> <div id="gutters-downspouts" class="ghb_toggle"> <p>Content 2</p> </div> <div id="operable-shades" class="ghb_toggle"> <p>Content 3</p> </div>

How can I modify my jQuery to be able to successfully interact with my anchor elements inside my <object> to hide/show a respective piece of content with a unique identifier?我如何修改我的 jQuery 以便能够成功地与我的<object>锚元素交互以隐藏/显示具有唯一标识符的相应内容?

To target the content of an svg document loaded inside an <object> , or <iframe> , you need to pass the correct Document context to jQuery: the one you can access from the .contentDocument property of the embedding element.要定位加载在<object><iframe>中的 svg 文档的内容,您需要将正确的 Document 上下文传递给 jQuery:您可以从嵌入元素的.contentDocument属性访问的上下文。

Note that this requires that the svg document has been loaded in a same-origin way, which is not possible in StackSnippets' null origined frames, so here is a live plnkr .请注意,这要求 svg 文档以同源方式加载,这在 StackSnippets 的空源帧中是不可能的,所以这里是一个实时 plnkr

$( "object" ).on( "load", (evt) => {
  const svg_doc = this.contentDocument;
  $( "a[href]", svg_doc ) // second argument is context
    .on( 'click', (evt) => {
      // add your listener here
    } );
};

Also note that in your svg you have xlink:href attributes being defined, not just href , so your jQuery selector should be 'a[xlink\\\\:href^="#"]'另请注意,在您的 svg 中,您定义了xlink:href属性,而不仅仅是href ,因此您的 jQuery 选择器应该是'a[xlink\\\\:href^="#"]'


But if your goal is to set the anchors in you svg relative to an other base URL than the one the document is loaded from, then you don't need all this, you can simply insert an HTML <base> element at the beginning of your svg document and all the relative URLs will use it.但是,如果您的目标是将 svg 中的锚点设置为相对于其他基本 URL 而不是加载文档的基本 URL,那么您不需要所有这些,您只需在开头插入一个 HTML <base>元素您的 svg 文档和所有相关 URL 都将使用它。
See live demo as a plnkr and a more complicated snippet:将现场演示视为plnkr和更复杂的片段:

 // To be able to embed it in a StackSnippet (directly in this answer), // we need to generate the file in memory from JS // you don't need it const svg_content = ` <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="300" > <!-- we must declare it in the HTML namespace --> <base xmlns="http://www.w3.org/1999/xhtml" href="https://google.com" /> <a href="#foo-bar"> <rect fill="green" x="10" y="10" width="30" height="30" /> </a> </svg> `; const svg_blob = new Blob( [ svg_content ], { type: "image/svg+xml" } ); const svg_url = URL.createObjectURL( svg_blob ); document.querySelector( "object" ).data = svg_url;
 <object></object>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM