用JS处理对象 [英] Manipulating objects with JS

查看:67
本文介绍了用JS处理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改SVG对象的属性.

I want change the attribut off a SVG object.

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:xlink="http://www.w3.org/1999/xlink">
<head>
<title>SVG use test</title>
<script type="text/javascript">
function setUsedFill(uId, fill) {
  document.getElementById(uId).instanceRoot.correspondingElement.setAttributeNS(null, 'fill', fill);
}
</script>
</head>
<body>
<div>
<input type="button" value="test" onclick="setUsedFill('uc1', 'yellow');"/>
<input type="button" value="test" onclick="setUsedFill('uc2', 'red');"/>
</div>
<div>
<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300">
  <defs>
    <circle id="c1" cx="50" cy="50" r="30" fill="green"/>
  </defs>
  <use id="uc1" x="0" y="0" xlink:href="#c1"></use>
  <use id="uc2" x="100" y="100" xlink:href="#c1"></use>
</svg>
</div>
</body>
</html>

此代码正在Opera,Chrome和IE 9中运行.

This Code is going on in Opera, Chrome and IE 9.

instanceRoot.correspondingElement-在Firefox/Mozilla中未运行

instanceRoot.correspondingElement - is not running in Firefox/Mozilla

推荐答案

在对另一个答案的评论中,您问还有其他选择吗?"

In a comment on another answer, you asked "is there any alternative?"

对于SVG工作,我使用的替代方法是 Raphael JavaScript库.

For SVG work, the alternative that I use is the Raphael javascript library.

这是一个出色的库,可用于处理JavaScript中的SVG grpahics和动画;使事情变得简单得多,而且,它甚至可以在某些真正的旧浏览器中使用,包括旧版本的IE,甚至可以追溯到IE6.

It is an excellent library for working with SVG grpahics and animations in Javascript; makes things a lot easier, and as an added bonus, it even works in some really old browsers -- including old versions of IE, as far back as IE6.

它与IE兼容的原因是,它可以透明地检测浏览器,并切换为使用VML而不是SVG绘制图形.但是从您作为开发人员的角度来看,您不需要了解这一点.您需要知道的是,它可以在所有浏览器中使用.亲爱的.

The reason it works with IE is because it transparently detects the browser and switches to drawing the graphics using VML instead of SVG. But from your perspective as a developer, you don't need to know about this; all you need to know is that it works in all browsers. Sweet.

它也不依赖于任何其他库;您不需要使用JQuery或其他任何东西来使用它(尽管如果您愿意的话,它也可以很好地与他们一起使用).

It also doesn't depend on any other libraries; you don't need to be using JQuery or anything else to use it (although it works just fine with them if you do want to).

我现在完全不使用纯SVG做任何工作;一切都是通过Raphael完成的.

I don't do any work at all now in pure SVG; everything is done via Raphael.

这篇关于用JS处理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆