我正在创建一个网页,用于检查用户提供的数据是否与网页脚本中的数据匹配。
我的代码:
var a = document.getElementById("a");
var b = document.getElementById("p");
function checking() {
if (b.innerHTML == a.value){
alert("All is perfect");
} else {
alert("Please check again");
}
}
<!DOCTYPE html>
<html>
<head>
<title>Terms and Conditions Checker</title>
</head>
<body>
<input id="a" autocomplete="off" />
<button onclick="checking()">Check</button>
<div id="p" style="display:none;">
a
b
</div>
</body>
</html>
所以,我想用<div>
内的空格替换行尾的空格。所以,如果代码是
<div id="p" style="display:none;">
a
b
</div>
它应匹配:
<div id="p" style="display:none;"> a b </div>
答案 0 :(得分:1)
删除所有标签,输入并使用正则表达式/(\r\n\t|\n|\r\t)/gm
见下面的代码:
var a = document.getElementById("a");
var b = document.getElementById("p");
function checking() {
var val = b.innerHTML.replace(/(\r\n\t|\n|\r\t)/gm, "");
console.log(a.value,val);
if (val == a.value){
alert("All is perfect");
} else {
alert("Please check again");
}
}
&#13;
<!DOCTYPE html>
<html>
<head>
<title>Terms and Conditions Checker</title>
</head>
<body>
<input id="a" autocomplete="off" />
<button onclick="checking()">Check</button>
<div id="p" style="display:none;">
a
b
</div>
</body>
</html>
&#13;