Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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 更改<;上的CSS3背景图像;p>;悬停_Javascript_Jquery_Css - Fatal编程技术网

Javascript 更改<;上的CSS3背景图像;p>;悬停

Javascript 更改<;上的CSS3背景图像;p>;悬停,javascript,jquery,css,Javascript,Jquery,Css,我试图使背景图像(我使用CSS3“背景”和“背景大小”设置)在我将鼠标悬停在某个段落上时发生变化。 我试过: jQuery中的 $(function () { $('#web').hover(function () { $(this).css('background', '#000 url(space-image.png) center center fixed no-repeat'); }) }); Javascript中的 onMouseOver="docu

我试图使背景图像(我使用CSS3“背景”和“背景大小”设置)在我将鼠标悬停在某个段落上时发生变化。 我试过:

jQuery中的

$(function () {
    $('#web').hover(function () {
        $(this).css('background', '#000 url(space-image.png) center center fixed no-repeat');
    })
});
Javascript中的

onMouseOver="document.getElementByName("body").style.backgroundColor = 'red';
还有一些人运气不好。

首先

$(function(){
    $('#web').hover( function(){
          $(this).css('background', '#000 url(space-image.png) center center fixed no-repeat');
     }); // <-- you missed this, meaning 'end of hover function'
});
目前,浏览器会认为
onmouseover
功能在
正文之前的
处停止。浏览器会将
和第二个
之间的所有内容设置为
onmouseover
。因此:

document.getElementByName(
这显然不起作用。您需要将第一个和最后一个
更改为
。这样,浏览器将把
之间的所有内容作为
onmouseover
值,该值首先起作用。

$(function(){
    $('#web').hover( function(){
          $(this).css('background', '#000 url(space-image.png) center center fixed no-repeat');
     }); // <-- you missed this, meaning 'end of hover function'
});
目前,浏览器会认为
onmouseover
功能在
正文之前的
处停止。浏览器会将
和第二个
之间的所有内容设置为
onmouseover
。因此:

document.getElementByName(

这显然不起作用。您需要将第一个和最后一个
更改为
。这样,浏览器将把
之间的所有内容作为
onmouseover
值来使用。

您是否考虑过完全使用CSS3伪类来实现这一点?换言之:

#web:hover {
    background: #000 url(space-image.png) center center fixed no-repeat;
}
编辑:


您想更改整个页面的背景图像还是只更改单个元素?如果是整个页面,那么您需要用$('body')替换$(this),因为$(this)只是指您在前一行中选择的#web元素。

您考虑过完全使用CSS3伪类吗?换言之:

#web:hover {
    background: #000 url(space-image.png) center center fixed no-repeat;
}
编辑:

您想更改整个页面的背景图像还是只更改单个元素?如果是整个页面,那么您需要将$('body')替换为$(this),因为$(this)只是指您在前一行中选择的#web元素。

此操作:

非jQuery代码的一些观察结果:

onMouseOver的
是指什么?是否将该字符串指定给变量?元素的
onmouseover
属性必须全部小写

您必须使用
\
对字符串中的引号进行转义。所以,
…(“body”)…
变成了
…(“body\”)…

没有
getElementByName
方法。有一个
getElementsByName
(复数),但是您真的给元素赋予了“body”的name属性吗?只需通过:
document.body
即可获得body元素

为什么要使用字符串,您应该使用函数:

myElement.onmouseover = function() {
    document.body.style.backgroundColor = "red";
};
这项工作:

非jQuery代码的一些观察结果:

onMouseOver的
是指什么?是否将该字符串指定给变量?元素的
onmouseover
属性必须全部小写

您必须使用
\
对字符串中的引号进行转义。所以,
…(“body”)…
变成了
…(“body\”)…

没有
getElementByName
方法。有一个
getElementsByName
(复数),但是您真的给元素赋予了“body”的name属性吗?只需通过:
document.body
即可获得body元素

为什么要使用字符串,您应该使用函数:

myElement.onmouseover = function() {
    document.body.style.backgroundColor = "red";
};
上面的示例包含如何使用鼠标悬停更改
背景色
字体大小


上面的示例包含如何使用鼠标悬停更改
背景色
字体大小

这对我来说似乎很好:这对我来说似乎很好: