Javascript jQuery:使用普通函数值作为jQuery值
本质上,我试图获取html中设置的值'col',并将其用作backgroundColor的值 以下是脚本:Javascript jQuery:使用普通函数值作为jQuery值,javascript,jquery,function,Javascript,Jquery,Function,本质上,我试图获取html中设置的值'col',并将其用作backgroundColor的值 以下是脚本: function bgc(col) { $("#BG") .animate({ backgroundColor: "col" }, 1000) } 以下是html: <div id="BG"> <a href="#" onclick="bgc(#ffffff);"></a> </div> 任何帮助都会很好,我已
function bgc(col) {
$("#BG") .animate({ backgroundColor: "col" }, 1000)
}
以下是html:
<div id="BG">
<a href="#" onclick="bgc(#ffffff);"></a>
</div>
任何帮助都会很好,我已经摆弄了一段时间,但我不能完全掌握它。
谢谢试试这个:
function bgc(col) {
$("#BG") .animate({ backgroundColor: col }, 1000)
}
及
您引用的地方不对:
function bgc(col) {
$("#BG") .animate({ backgroundColor: col }, 1000)
}
<div id="BG">
<a href="#" onclick="bgc('#ffffff');"></a>
</div>
函数bgc(col){
$(“#BG”).animate({backgroundColor:col},1000)
}
另外,更好的做法是将标记和javascript分开。请注意,如果启用了javascript,这将如何优雅地降低性能
$(function() {
$('#BG').find('a').show().click( function() {
$('#BG').animate( { backgroundColor: '#ffffff' }, 1000 );
});
});
<div id="BG">
<a href="#" style="display: none;"></a>
</div>
$(函数(){
$('#BG')。查找('a')。显示()。单击(函数(){
$('#BG')。动画({backgroundColor:'#ffffff'},1000);
});
});
我正在使用一个完整的链接列表,并且我正在为每个链接设置颜色,我可以通过在脚本中用颜色写出每个链接的函数来做到这一点,但这是毫无意义的代码复制,并不能解决我要做的事。@Shufflshed——您可以使用类来实现吗?为每个链接类别分配一个类,然后根据这些类设置处理程序?
$(function() {
$('#BG').find('a').show().click( function() {
$('#BG').animate( { backgroundColor: '#ffffff' }, 1000 );
});
});
<div id="BG">
<a href="#" style="display: none;"></a>
</div>