Javascript 使用jquery在CSS中设置背景图像
我正在尝试使用jquery为我的一个html元素设置背景图像Javascript 使用jquery在CSS中设置背景图像,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试使用jquery为我的一个html元素设置背景图像 <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
<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;");
});
$(“#globalsearchstr”).focus(函数(){
$(this.parent().css(“背景”,“url(/images/r-srchbg_white.png)no repeat;”);
});
但这永远不会起作用。在焦点上唯一发生的变化是在HTML中添加了一个样式属性,如下所示
<div class="rmz-srchbg" style="">
</div>
CSS没有变化。试试这个
$("#globalsearchstr").focus(function(){
$(this).parent().css("background", "url('../images/r-srchbg_white.png') no-repeat");
});
试试这个:
<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>
$(函数(){
$('#globalsearchstr')。on('focus mouseenter',function(){
$(this.parent().css(“背景”,“url(/images/r-srchbg_white.png)无重复”);
});
});
删除url中禁止重复后的分号,然后重试
$("#globalsearchstr").focus(function(){
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
});
使用:
而不是
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat;");
你可以看到更多的例子,试试这个
$(this).parent().css("backgroundImage", "url('../images/r-srchbg_white.png') no-repeat");
您必须删除css规则字符串中的分号:
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
使用绝对路径怎么样?在控制台中检查您的相对路径是否为accurateCSS$(this).css('background-image','url('+imageUrl+')no repeat');这不起作用背景图像是错误的,它应该是backgroundImage不起作用。仍然是相同的问题(在问题中解释)@Athul现在试一试,错误是什么?要在jquery中使用背景图像
,您应该使用背景图像
!两种方法都可以接受。当然可以。“背景”属性不正确。“背景图片”你想说的。谢谢分享+对于url()
中的'
,为1。这很有魅力。
$(this).parent().css("background", "url(/images/r-srchbg_white.png) no-repeat");
<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>
$(document).ready(function() {
$('#globalsearchstr').bind('mouseenter', function() {
$(this).parent().css("background", "black");
});
});