Javascript 使用 jQuery 为属性设置新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11794105/
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
Setting new value for an attribute using jQuery
提问by Sakshi Sharma
I am trying to set a new vale of a custom attribute of a div using attr()
. I found out it can be done using .attr( attributeName, value )
, but when I try it it's not working.
我正在尝试使用 .div 设置 div 的自定义属性的新值attr()
。我发现它可以使用 来完成.attr( attributeName, value )
,但是当我尝试它时它不起作用。
Here is the part of my code I am interested in:
这是我感兴趣的代码部分:
$('#amount').attr( 'datamin','1000')
and the divthat has the custom attribute is
并且具有自定义属性的div是
<div id="amount" datamin=""></div>
Here is the whole example:
这是整个示例:
$(function() {
$( "#slider-range" ).slider({
range: true,
min: <?php echo $min_val;?>,
max: <?php echo $max_val;?>,
values: [ <?php echo $min_val;?>, <?php echo $max_val;?> ],
slide: function( event, ui ) {
$( "#amount" ).html( "<?php echo $currency_chk_i;?>" + ui.values[ 0 ] + " - <?php echo $currency_chk_i;?>" + ui.values[ 1 ] );
$('#amount').attr( 'datamin','1000');
},
change: function(event, ui) {
// when the user change the slider
},
stop: function(event, ui) {
alert(ui.values[0]);
// when the user stopped changing the slider
}
});
$( "#amount" ).html( "<?php echo $currency_chk_i;?>" + $( "#slider-range" ).slider( "values", 0 ) +
" - <?php echo $currency_chk_i;?>" + $( "#slider-range" ).slider( "values", 1 ) );
});
Can anyone point out where I am wrong or any other way out?
谁能指出我错在哪里或任何其他出路?
采纳答案by Blowsie
Works fine for me
对我来说很好用
See example here. http://jsfiddle.net/blowsie/c6VAy/
请参阅此处的示例。 http://jsfiddle.net/blowsie/c6VAy/
Make sure your jquery is inside $(document).ready
function or similar.
确保您的 jquery 在$(document).ready
函数内部或类似的内部。
Also you can improve your code by using jquery data
您也可以使用 jquery数据改进您的代码
$('#amount').data('min','1000');
<div id="amount" data-min=""></div>
Update,
更新,
A working example of your full code (pretty much) here. http://jsfiddle.net/blowsie/c6VAy/3/
您的完整代码(几乎)的工作示例在这里。 http://jsfiddle.net/blowsie/c6VAy/3/