jQuery css()无法与字符串一起使用 [英] jquery css() is not working with a string

查看:108
本文介绍了jQuery css()无法与字符串一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var csn = ['font-family', 'font-size', 'font-weight', 'color', 'text-decoration', 'text-shadow', 'background-color'];

$.each(csn, function (n, value) {
    style.push("'" + value + "':'" + $('div').css(value) + "'");
});

var msg = "{" + style.join(',') + "}";

$('div#taget').css(msg);

推荐答案

您应将一个对象传递给css方法. '{...}'这是一个字符串.这样,您将css方法用作获取器而不是设置器,jQuery尝试查找并返回所请求属性的值(那个'{...}'字符串),这当然是undefined.

You should pass an object to the css method. '{...}' this is a string. This way you are using the css method as a getter and not a setter, jQuery tries to find and return the value of the requested property(that '{...}' string), which is of course undefined.

这篇关于jQuery css()无法与字符串一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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