使用JavaScript的textContent属性获取元素内的文本内容,并比较这两个文本内容是否相同,示例代码如下:
Hello, world!
Hello, world!
Goodbye, world!
const text1 = document.getElementById("text1").textContent;
const text2 = document.getElementById("text2").textContent;
const text3 = document.getElementById("text3").textContent;
if (text1 === text2) {
console.log("text1 and text2 are the same.");
} else {
console.log("text1 and text2 are NOT the same.");
}
if (text1 === text3) {
console.log("text1 and text3 are the same.");
} else {
console.log("text1 and text3 are NOT the same.");
}
输出结果为:
text1 and text2 are the same.
text1 and text3 are NOT the same.
下一篇:比较两个元组的前两个参数。