为 font-weight 设置 jQuery 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6383151/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:45:45  来源:igfitidea点击:

Setting jQuery attribute for font-weight

jquery

提问by JennyM

I am trying to change the font-weight of an element. I tried with the following but it doesn't seem to work:

我正在尝试更改元素的字体粗细。我尝试了以下操作,但似乎不起作用:

$("#opt_" + i).attr("font-weight", "bold");

Also what's the difference between prop and attr? Is that something to do with my problem?

还有 prop 和 attr 有什么区别?是不是跟我的问题有关?

回答by Alo

use the .css()function instead of .attr()

使用.css()函数代替 .attr()

$("#opt_" + i).css("font-weight", "bold")

回答by Niklas

It's not an attribute, but CSS:

它不是属性,而是 CSS:

$("#opt_" + i).css("font-weight", "bold");

example: http://jsfiddle.net/niklasvh/ENwbn/

示例:http: //jsfiddle.net/niklasvh/ENwbn/

回答by devmatt

$("#opt_" + i).style("font-weight", "bold");

回答by Andreas Niedermair

Also what's the difference between prop and attr?

还有 prop 和 attr 有什么区别?

according to the relase notes.prop()should be used for dom-properties, whereas .attr()should be used for html-attributes:

根据relase 注释.prop()应该用于 dom-properties,而.attr()应该用于 html-attributes:

First, using the .attr() method on the window or document did not work in jQuery 1.6 because the window and document cannot have attributes. They contain properties (such as location or readyState) that should be manipulated with .prop() or simply with raw javascript. In jQuery 1.6.1, the .attr() will defer to the .prop() method for both the window and document instead of throwing an error.

首先,在窗口或文档上使用 .attr() 方法在 jQuery 1.6 中不起作用,因为窗口和文档不能有属性。它们包含应该使用 .prop() 或仅使用原始 javascript 操作的属性(例如 location 或 readyState)。在 jQuery 1.6.1 中, .attr() 将遵循 .prop() 窗口和文档的方法,而不是抛出错误。

回答by Ajay2707

$('#labelAmt').css("font-weight", "bold").css("color", "firebrick");

I just gave an answer because if somebody wants to set multiple properties in a single statement.

我只是给出了一个答案,因为如果有人想在一个语句中设置多个属性。

So rather than above statement, you can set with comma separated value. Here it is syntax:

因此,您可以使用逗号分隔值进行设置,而不是上面的语句。这是语法

$('#labelAmt').attr({"font-weight": "bold", "data-test-2": val2}); //you can set your custom attributes too.