javascript jQuery attr() 无法设置属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6556368/
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
jQuery attr() fails to set attribute
提问by Majid Fouladpour
I am trying to rotate an image via svg's transform
. This is the code I have:
我正在尝试通过 svg 的transform
. 这是我的代码:
<svg width="100" height="100">
<image id="trns" transform="rotate(90,50,50)" width="100" height="100" xlink:href="logo.png"/>
</svg>
This successfully rotates logo.png
by 90 degrees when the page loads. Also, when I change 90
to a different number in firbug's HTML tab the rotation changes accordingly. But when I try to change the value with jQuery, nothing happens:
logo.png
当页面加载时,这成功地旋转了 90 度。此外,当我90
在 firbug 的 HTML 选项卡中更改为不同的数字时,旋转会相应地更改。但是当我尝试使用 jQuery 更改值时,没有任何反应:
$('#trns').attr('transform', 'rotate(60, 50,50)');
What does firebug do that my attr
line does not?
firebug 做了什么而我的attr
线路没有?
采纳答案by Niklas
Working fine here (with jQuery 1.6.2): http://jsfiddle.net/niklasvh/k3Grd/
在这里工作正常(使用 jQuery 1.6.2):http: //jsfiddle.net/niklasvh/k3Grd/
Make sure to call it once the DOM is ready:
确保在 DOM 准备好后调用它:
$(function(){
$('#trns').attr('transform', 'rotate(60,50,50)');
});
回答by Freek
Very strange indeed, this seems to work
确实很奇怪,这似乎有效
$('#trns')[0].setAttribute('transform','rotate(20,50,50)')
Also, if u look at $('#trns').attr('transform'), you are getting an object.. Not enough time to look into that now.
此外,如果您查看 $('#trns').attr('transform'),您将得到一个对象.. 现在没有足够的时间来研究它。
回答by Edgar Villegas Alvarado
If you're using jquery >= 1.6 try using prop
instead of attr
.
Hope this helps. Cheers
如果您使用的是 jquery >= 1.6,请尝试使用prop
而不是attr
.
希望这可以帮助。干杯
回答by Allan
.attr() works in 3.1.1 like this
.attr() 像这样在 3.1.1 中工作
.attr({ style: "prop : val; prop : val; prop : val" })
this might be a way around version problems.
这可能是解决版本问题的一种方法。