简体   繁体   English

使用 jQuery 获取 SVG 中锚点的 href 值

[英]Getting the href value of an anchor within an SVG using jQuery

I am using jQuery to target anchor elements within an SVG image I have created to get the value of the href assigned to the anchor element.我正在使用 jQuery 来定位我创建的 SVG 图像中的锚元素,以获取分配给锚元素的 href 值。 I am then trying to toggle on and off a piece of content that has an HTML ID identical to the href of the anchor element.然后,我尝试打开和关闭具有与锚元素的 href 相同的 HTML ID 的一段内容。

 jQuery(document).ready(function ($) { $('.ghb_toggle').hide() $('object').on( "load", (evt) => { // wait for the page has fully loaded const svg_doc = $("object")[0].contentDocument; $('a[xlink\\\\:href^="#"]', svg_doc ) .on( "click", (evt) => { target = evt.currentTarget.href; console.log(target); // for debugging purposes $(target).toggle(); }); }); });
 <object data="/wp-content/uploads/GHB_Interface-v0.1.svg" width="1400" height="1200"></object> <div class="ghb_toggle" id="gable-pediments">content1</div> <div class="ghb_toggle" id="gutters-downspouts">content2</div> <div class="ghb_toggle" id="operable-shades">content3</div>

I am facing a problem when I assign the href value to the variable I created "target".当我将 href 值分配给我创建的“目标”变量时,我遇到了问题。 When I output the value of my variable $(target) in the console:当我在控制台中输出变量 $(target) 的值时:

target = evt.currentTarget.href;
console.log(target);

Output:输出:

SVGAnimatedString {baseVal: "#gutters-downspouts", animVal: "#gutters-downspouts"}

This seems like it is successfully getting the href value of my anchor element!这似乎成功获取了我的锚元素的 href 值! (the value should be "#gutters-downspouts"). (该值应为“#gutters-downspouts”)。

However, when I try to use jQuery to then toggle the respective html element with an ID that is identical to the href value like this:但是,当我尝试使用 jQuery 然后使用与 href 值相同的 ID 切换相应的 html 元素时,如下所示:

target = evt.currentTarget.href;
$(target).toggle();

I get the error:我收到错误:

Uncaught TypeError: Cannot read property 'display' of undefined

What the heck am I doing wrong?我到底做错了什么? It seems as if it thinks my variable is undefined, however, I just printed it in the console and saw it had a value!似乎它认为我的变量未定义,但是,我只是在控制台中打印了它并看到它有一个值!

From the MDN documentation https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimatedString从 MDN 文档https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimatedString

SVGAnimatedString.animVal Read only This is a DOMString representing the animation value. SVGAnimatedString.animVal 只读 这是一个表示动画值的 DOMString。 If the given attribute or property is being animated it contains the current animated value of the attribute or property.如果给定的属性或属性正在被动画,它包含属性或属性的当前动画值。 If the given attribute or property is not currently being animated, it contains the same value as baseVal.如果给定的属性或属性当前未设置动画,则它包含与 baseVal 相同的值。

SVGAnimatedString.baseVal This is a DOMString representing the base value. SVGAnimatedString.baseVal 这是一个表示基值的 DOMString。 The base value of the given attribute before applying any animations.应用任何动画之前给定属性的基值。

So you are successfully getting the information you need but it's looking like an object with two entries.因此,您已成功获取所需信息,但它看起来像一个包含两个条目的对象。 Try extracting the actual target value by using target.baseVal尝试使用 target.baseVal 提取实际目标值

(apologies that I have been unable to test this on a real system). (抱歉,我无法在真实系统上对此进行测试)。

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

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