jQuery attr() 无法设置属性 [英] jQuery attr() fails to set attribute

查看:38
本文介绍了jQuery attr() 无法设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 svg 的 transform 旋转图像.这是我的代码:

I am trying to rotate an image via svg's transform. This is the code I have:

<svg width="100" height="100"> 
   <image id="trns" transform="rotate(90,50,50)" width="100" height="100" xlink:href="logo.png"/> 
</svg>

这在页面加载时成功地将 logo.png 旋转了 90 度.此外,当我在 firbug 的 HTML 选项卡中将 90 更改为不同的数字时,旋转会相应地更改.但是当我尝试使用 jQuery 更改值时,没有任何反应:

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:

$('#trns').attr('transform', 'rotate(60, 50,50)');

firebug 做了什么而我的 attr 行没有做?

What does firebug do that my attr line does not?

推荐答案

在这里工作正常(使用 jQuery 1.6.2):http://jsfiddle.net/niklasvh/k3Grd/

Working fine here (with jQuery 1.6.2): http://jsfiddle.net/niklasvh/k3Grd/

确保在 DOM 准备好后调用它:

Make sure to call it once the DOM is ready:

$(function(){
 $('#trns').attr('transform', 'rotate(60,50,50)');
});

这篇关于jQuery attr() 无法设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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