Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery在CSS中设置背景图像_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 使用jquery在CSS中设置背景图像

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="&nbsp;" id="srchbtn" class="rmz-srchico"> <br

我正在尝试使用jquery为我的一个html元素设置背景图像

<div class="rmz-srchbg">
    <input type="text" id="globalsearchstr" name="search" value="" class="rmz-txtbox">
    <input type="submit" value="&nbsp;" 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="&nbsp;" 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="&nbsp;" id="srchbtn" class="rmz-srchico">
  <br style="clear:both;">
</div>
$(document).ready(function() {
  $('#globalsearchstr').bind('mouseenter', function() {
    $(this).parent().css("background", "black");
  });
});