Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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/7/css/34.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
Can';t使用jQuery更改身体中的背景图像_Jquery_Css - Fatal编程技术网

Can';t使用jQuery更改身体中的背景图像

Can';t使用jQuery更改身体中的背景图像,jquery,css,Jquery,Css,我试图用jQuery替换身体背景中的图像,但不管我做什么,它都不起作用。代码与其他问题或教程中的代码几乎相同。 以下是有效的CSS: body{ background-image: url('img/1.jpg'); } 但这里的jQuery没有: $('body').css('background-image', 'url(img/2.jpg)'); 和HTML: <html lang="pl"> <head> <script src="htt

我试图用jQuery替换身体背景中的图像,但不管我做什么,它都不起作用。代码与其他问题或教程中的代码几乎相同。 以下是有效的CSS:

body{
     background-image: url('img/1.jpg');
}
但这里的jQuery没有:

$('body').css('background-image', 'url(img/2.jpg)');
和HTML:

 <html lang="pl">
 <head>
 <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
 </script>
 <script src="script.js"></script>
 <meta charset="UTF-8">

 <link rel="stylesheet" href="styles.css">
 <Title> Moja firma </Title>
 </head>
 <body>

 </body>
 </html>

墨汁
我检查了,jQuery和文件及其代码都被加载了。
我还试着在url中加引号,但什么都没有。

当我测试jquery时,您的代码似乎工作得很好。 所以我建议把你的
移到你身体标签的底部

<body>
    stuff for page

    <script src="script.js"></script>
</body>

当我测试jquery时,您的代码似乎运行良好。 所以我建议把你的
移到你身体标签的底部

<body>
    stuff for page

    <script src="script.js"></script>
</body>

你可以试试下面的方法

我会为每一页这样做

<body class="home">
<body class="about">
<body class="contact">
点击按钮,您可以添加下面的js

$(function(){
    $('#aboutlink').click(function(e){
        $('body').removeClass().addClass('about');

        //your other snippets here

        e.preventDefault();
    });
});

你可以试试下面的方法

我会为每一页这样做

<body class="home">
<body class="about">
<body class="contact">
点击按钮,您可以添加下面的js

$(function(){
    $('#aboutlink').click(function(e){
        $('body').removeClass().addClass('about');

        //your other snippets here

        e.preventDefault();
    });
});

工作示例只需将代码包装在document.ready函数中即可

$(函数(){
$(“body”).css('background-image','url(https://recruiterflow.com/blog/wp-content/uploads/2017/10/stackoverflow.png)');
});

墨汁

工作示例只需将代码包装在document.ready函数中即可

$(函数(){
$(“body”).css('background-image','url(https://recruiterflow.com/blog/wp-content/uploads/2017/10/stackoverflow.png)');
});

墨汁

触发更改的事件是什么。如果您未触发事件,则更改不会被应用。例如:

加载文档时:

$(document).ready(function(){
$('body').css('background-image', "url('img/2.jpg')");
})
当你按下按钮时

$('button').on("click", function(){
 $('body').css("background-image", "url('img/2.jpg')");
})
下面是一个工作示例

还要检查符合您需要的活动


触发更改的事件是什么。如果您未触发事件,则更改不会被应用。例如:

加载文档时:

$(document).ready(function(){
$('body').css('background-image', "url('img/2.jpg')");
})
当你按下按钮时

$('button').on("click", function(){
 $('body').css("background-image", "url('img/2.jpg')");
})
下面是一个工作示例

还要检查符合您需要的活动


这可能只是您的值格式错误的问题。尝试将其更改为
'url(“img/2.jpg”)”
,使该值具有引号,就像css规则一样。我写道,我尝试了,但不起作用。然后,您可以编辑您的问题,以生成该问题的工作示例吗?我看不出你所提供的有任何其他突出的问题。在我的代码中实际上什么都没有,只是用CSS和JS Embedded清除HTML,但我可以添加itOkay,jasinth premkumar works的建议,谢谢!这可能只是您的值格式错误的问题。尝试将其更改为
'url(“img/2.jpg”)”
,使该值具有引号,就像css规则一样。我写道,我尝试了,但不起作用。然后,您可以编辑您的问题,以生成该问题的工作示例吗?我看不出你所提供的有任何其他突出的问题。在我的代码中实际上什么都没有,只是用CSS和JS Embedded清除HTML,但我可以添加itOkay,jasinth premkumar works的建议,谢谢!