简体   繁体   English

jquery更改div类的样式属性

[英]jquery to change style attribute of a div class

I have a slider class like this and I wan to change the style attribute style="left: 336px"我有一个这样的滑块类,我想更改样式属性style="left: 336px"

<div id="range-cont">
<div class="slider">
<div class="progress" style="width: 350px;"></div>
    <a class="handle" href="#" **style="left: 336px;"**></a>
</div>
<input id="percentage" class="range" type="range" value="14" name="percentage" min="0" max="100">
<p id="percent-label">%</p>
</div>
</div>

I tried $('.handle').css({'style':'left: 300px'}) but its not working.我试过$('.handle').css({'style':'left: 300px'})但它不起作用。 Not sure what I am doing wrong here不确定我在这里做错了什么

试试$('.handle').css('left', '300px');

if you just want to set the style attribute use如果您只想设置样式属性,请使用

$('.handle').attr('style','left: 300px');

Or you can use css method of jQuery to set only one css style property或者你可以使用 jQuery 的 css 方法只设置一个 css 样式属性

$('.handle').css('left', '300px');

OR same as key value OR 与键值相同

$('.handle').css({'left': '300px', position});

More info on W3schools关于 W3schools 的更多信息

Try with试试

$('.handle').css({'left': '300px'});

Instead of代替

$('.handle').css({'style':'left: 300px'})
$('.handle').css('left', '300px');

$('.handle').css({
    left : '300px'
});

$('.handle').attr('style', 'left : 300px');

or use OrnaJS或使用 OrnaJS

$('.handle').css('left','300px') try this, identificator first then the value $('.handle').css('left','300px')试试这个,先是标识符然后是值

more on this here:更多关于这里:

http://api.jquery.com/css/ http://api.jquery.com/css/

这对你有帮助..

$('.handle').css('left', '300px');

Style is an attribute so css won't work for it.U can use attr样式是一个属性,所以css不适合它。你可以使用attr

Change:改变:

$('.handle').css({'style':'left: 300px'});

T0:时间:

$('.handle').attr('style','left: 300px');//Use `,` Comma instead of `:` colon

You can't use $('#Id').attr('style',' color:red');你不能使用$('#Id').attr('style',' color:red'); and $('#Id').css('padding-left','20%');$('#Id').css('padding-left','20%');
at the same time.同时。
You can either use attr or css but both only works when they are used alone.您可以使用attrcss但两者仅在单独使用时才有效。

In order to change the attribute of the class conditionally,为了有条件地改变类的属性,

var css_val = $(".handle").css('left');
if(css_val == '336px')
{
  $(".handle").css('left','300px');
}

If id is given as following,如果 id 给出如下,

<a id="handle" class="handle" href="#" style="left: 336px;"></a>

Here is an alternative solution:这是一个替代解决方案:

var css_val = $("#handle").css('left');
if(css_val == '336px')
{
  $("#handle").css('left','300px');
}

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

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