Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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 什么是'+';“登录”;url(“图像url”);?_Javascript_Jquery_Css - Fatal编程技术网

Javascript 什么是'+';“登录”;url(“图像url”);?

Javascript 什么是'+';“登录”;url(“图像url”);?,javascript,jquery,css,Javascript,Jquery,Css,正在阅读一些jquery代码表单。+符号和空格在“url(“+imageUrl+”)中起什么作用 我不熟悉jquery和js <!DOCTYPE html> <html lang="en"> <head> <title>jQuery Setting background-image CSS Property</title> <style> .box{ width: 500px;

正在阅读一些jquery代码表单。+符号和空格在“url(“+imageUrl+”)中起什么作用

我不熟悉jquery和js

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Setting background-image CSS Property</title>
<style>
    .box{
        width: 500px;
        height: 300px;
        border: 5px solid #333;
    }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    // Set background image of a div on click of the button
    $("button").click(function(){
        var imageUrl = "images/sky.jpg";
        $(".box").css("background-image", "url(" + imageUrl + ")");
    });    
});
</script>
</head>
<body>
    <div class="box"></div>
    <p><button type="button">Set Background Image</button></p>
</body>
</html><!DOCTYPE html>

jQuery设置背景图像CSS属性
.盒子{
宽度:500px;
高度:300px;
边框:5px实心#333;
}
$(文档).ready(函数(){
//单击按钮即可设置div的背景图像
$(“按钮”)。单击(函数(){
var imageUrl=“images/sky.jpg”;
$(“.box”).css(“背景图像”,“url”(+imageUrl+));
});    
});
设置背景图像


在Javascript中,可以使用
+
符号连接字符串

比如说,

let string1 = Hello;
let string2 = World;
let fullString = string1 + string2; // This is now: HelloWorld.
因此,基本上您的代码只是将
imageUrl
附加到
url()

最后你会得到类似于
url(“images/sky.jpg”)

plus(
++
)操作符在JavaScript中有几种用法。加号运算符执行的实际操作由运算符所在的上下文定义

在数字上下文中,此运算符用于加法

1+2//3

在字符串上下文中,它用于连接
'1'+'2'/'12'

1+'2'/'12'

将其添加到字符串中。