使用jquery在CSS中设置背景图像 [英] Set background image in CSS using jquery
本文介绍了使用jquery在CSS中设置背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用jquery设置我的html元素的背景图片
I am trying to set the background image for one of my html element using jquery
<div class="rmz-srchbg">
<input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
<input type="submit" value=" " id="srchbtn" class="rmz-srchico">
<br style="clear:both;">
</div>
$("#globalsearchstr").focus(function(){
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat;");
});
但这从不工作。只有改变发生的事情是,样式属性被添加到HTML,
but this never works.On focus only change happens is that a style attribute is added to HTML, like this
<div class="rmz-srchbg" style="">
</div>
不会改变CSS。
推荐答案
尝试以下操作:
<div class="rmz-srchbg">
<input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
<input type="submit" value=" " id="srchbtn" class="rmz-srchico">
<br style="clear:both;">
</div>
<script>
$(function(){
$('#globalsearchstr').on('focus mouseenter', function(){
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
});
});
</script>
这篇关于使用jquery在CSS中设置背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文