您的位置:首页 > Web前端 > JQuery

源码-JavaScript&jQuery交互式前端开发-第2章-JavaScript基础指令-在字符串中使用引号

2016-09-26 23:21 1031 查看
字符串必须被放置在单引号或双引号中,如要在字符串中使用双引号,必须使用单引号来包含整个字符串。

JS 代码:

// Create variables to hold the title and note text.
var title;
var message;

// Assign values to these variables.
title = "Molly's Special Offers";
<strong>message = '<a href=\"sale.html\">25% off!</a>';</strong>

// Get the element with an id of title.
var elTitle = document.getElementById('title');
// Replace the content of this element.
elTitle.textContent = title;

// Get the element with an id of note.
var elNote = document.getElementById('note');
// Replace the content of this element.
elNote.innerHTML = message;

/*
NOTE: textContent does not work in IE8 or earlier
You can use innerHTML on line 12, but note the security issues on p228-231
elTitle.innerHTML = title;

innerHTML is used on line 17 because it is adding markup (not just text)
*/


HTML代码:

<!DOCTYPE html>
<html>
<head>
<title>JavaScript & jQuery - Chapter 2: Basic JavaScript Instructions - String With Quotes</title>
<link rel="stylesheet" href="css/c02.css" />
</head>
<body>
<h1>Elderflower</h1>
<div id="content">
<div id="title">Special Offers</div>
<div id="note">Sign-up to receive personalized offers!</div>
</div>
<script src="js/string-with-quotes.js"></script>
</body>
</html>


CSS代码:(参考:源码-JavaScript&jQuery交互式前端开发-第2章-JavaScript基础指令-使用变量来存储数字,http://blog.csdn.net/hpdlzu80100/article/details/52669264


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐