声明字符串类型变量

字符串是存储字符(比如 "Bill Gates")的变量。字符串可以是引号中的任意文本。可以使用单引号或双引号。

请在预置代码中编写Javascript代码,使预览页面显示如下内容:

It's alright
He is called 'Bill'
He is called "Bill"

注:可以在字符串内使用引号,只要这些引号与包围字符串的引号不匹配

任务与要求

请定义字符串变量str1,使用双引号,赋值It's all right

请定义字符串变量str2,使用双引号,赋值He is called 'Bill'

请定义字符串变量str3,使用单引号,赋值He is called "Bill"

请使用document.getElementById(id)方法,并结合br换行标签,使id为demo的段落分行显示3个变量值

<!DOCTYPE html>

<html>

<body>

<p id="demo"></p>

<script>

</script>

</body>

</html>


qq_慕标6304929
浏览 656回答 1
1回答

莲_蓶濏__

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <p id="demo"></p> <script> for (var i = 1, str1 = "It's all right", str2 = "He is called 'Bill'", str3 = 'He is called "Bill"', oIdDmo = document.getElementById('demo'); i <= 3; i++) oIdDmo.innerHTML += window['str' + i] + '<br />'; </script> </body> </html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript