简体   繁体   English

使用jquery设置自定义属性和值

[英]set custom attribute and value with jquery

<div class='progress-20'>

我想用其他值改变进度-20,我通过自定义表达式得到了值。

You can use attr() to set the class attribute of the element. 您可以使用attr()来设置元素的class属性。 You need to select the div for that you can use class selector . 您需要选择可以使用类选择器的div。

$('.progress-20').attr('class','newclass');

Edit based on comments. 根据评论进行编辑

You can use starts with attribute selector if 20 could change 如果20可以更改,则可以使用带属性选择器的开头

$('[class^=progress-]').attr('class','newclass');
$('.progress-20').attr('class','new_value')

尝试toggleClass()之类的,

$('div').toggleClass('progress-20 newclass');

Try this Demo: http://jsfiddle.net/Ja2SU/ 试试这个演示: http//jsfiddle.net/Ja2SU/

OR this demo http://jsfiddle.net/bFLju/ 或者这个演示 http://jsfiddle.net/bFLju/

*API: * API:

you could use attr or removeClass and then addClass 你可以使用attrremoveClass然后使用addClass

Gave you few choices hope anyone will fit you need :) 给你几个选择希望任何人都适合你需要:)

Code

$(function () {
    alert(' before altering => ' + $('div').attr('class'));
    $('div').attr('class', 'foobar');
    alert(' after altering => ' + $('div').attr('class'));
});

or 要么

$(function () {
    $('div').removeAttr('class').addClass('foobar');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM