JS中replace的用法
要求在JS中编写一个functionreplace(tag,value),definingformat()function,通过点击Format按钮,将<spanid="...
要求在JS中编写一个function replace(tag,value),defining format()function,通过点击Format按钮,将 <span
id="salutation">后的Name替换成'Mr. Smith',
<span id="invoice">后的ID 替换成 123,
<span id="time">后的some time 和the material替换成flux capacitor。
如果tag不存在则弹出警告框“不存在”。
如果我想改变每个span后所有单词的首字母改为大写该如何写程序?
<body>
Dear <span
id="salutation">Name</span>;
<p>
It has come to our attention that your invoice
<span id="invoice">ID</span>
has yet to be paid. It has now been <span
id="time">some time</span> since
you received <span id="item">the
material</span> from Evil Incorporated. Please
remit payment immediately. <span
id="threaten"></span>
</p>
Yours sincerely,<br>
<br>
<br>
J. Smith, Accounting
<div id="buttons">
<center>
<button
onclick="format()">Format</button>
<button
onclick="clearit()">Clear</button>
</center>
</div>
</body> 展开
id="salutation">后的Name替换成'Mr. Smith',
<span id="invoice">后的ID 替换成 123,
<span id="time">后的some time 和the material替换成flux capacitor。
如果tag不存在则弹出警告框“不存在”。
如果我想改变每个span后所有单词的首字母改为大写该如何写程序?
<body>
Dear <span
id="salutation">Name</span>;
<p>
It has come to our attention that your invoice
<span id="invoice">ID</span>
has yet to be paid. It has now been <span
id="time">some time</span> since
you received <span id="item">the
material</span> from Evil Incorporated. Please
remit payment immediately. <span
id="threaten"></span>
</p>
Yours sincerely,<br>
<br>
<br>
J. Smith, Accounting
<div id="buttons">
<center>
<button
onclick="format()">Format</button>
<button
onclick="clearit()">Clear</button>
</center>
</div>
</body> 展开
1个回答
推荐于2016-10-11
展开全部
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script>
function replace(tag, value) {
return String.prototype.replace.call(value, tag, arguments[2]);
}
function format() {
var html = document.body.innerHTML;
var tag = "salutation|invoice|time|item|threaten";
var reg = new RegExp("(<span[^>]*id[=\"\'\\s]+(" + tag + ")[\"\'\\s]+>)((?:(?!<\\/span>)[\\s\\S])*)(<\\/span>)", "gi");
html = replace(reg, html, function(a, a1, a2, a3, a4) {
var str = "";
if (a2 == "salutation") {
str = "Mr. Smith";
} else if (a2 == "invoice") {
str = 123;
} else if (a2 == "item" || a2 == "time") {
str = 'flux capacitor';
} else if (a2 == "threaten") {
str = 'Please do not make me angry.';
}
tag = tag.replace(new RegExp("\\|?" + a2 + "\\|?", "g"), "|").replace(/^\||\|$/g, "");
var result = a1 + str + a4;
return result;
});
//改变每个span后所有单词的首字母改为大写
reg = /(<span[^>]*>)((?:(?!<\/span>)[\s\S])*)(<\/span>)/ig;
html = replace(reg, html, function(a, b, m, n) {
return b + replace(/([a-z])([^\r\n\f\x20\t]*)/gi, m, function(c, d, e) {
return d.toUpperCase() + e;
}) + n;
});
//如果tag不存在则弹出警告框“不存在”
tag ? alert(tag.replace(/\|/g, ",") + "不存在") : 0;
document.body.innerHTML = html;
}
//编写一个函数clearit(),从页面中删除所有包含在标记id =“buttons”的内容
function clearit() {
var btns = document.getElementById("buttons");
btns.innerHTML = "";
}
</script>
</head>
<body>Dear <span id="salutation">Name</span>:
<p>It has come to our attentation that your invoice <span id="invoice">ID</span>
has yet to be paid.It has now been <span id="time">some time</span> since you received <span id="item">the material</span> from Evil Incorporated. Please remit payment immediately. <span id="threaten"></span>
</p>Yours sincerely.
<br />
<br />
<br />J.Smith,Accounting
<div id="buttons">
<center>
<button onclick="format()">Format</button>
<button onclick="clearit()">Clear</button>
</center>
</div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询