为什么console.log
语句返回未定义?
即使标签的id
是 right ,所以它应该返回存储在<p>
标签中的值?
x=document.getElementById("pppp").value //assigning the value of <p> tag to x
console.log(x) //printing the value of x in console
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>blabla</title>
</head>
<body>
<p id="pppp">dvshjb</p>
</body>
</html>
答案 0 :(得分:0)
value
属性,用于访问VALUE
的{{1}}。
对于您而言,我假设您正在尝试访问Element的文本。 Node.textContent
在这里很理想。
Form Element
var x = document.getElementById("pppp").textContent;
console.log(x);
答案 1 :(得分:0)
p标签没有任何值属性。看来您需要文字内容。在这种情况下,请使用innerHTML
。 trim()将用于删除空白
let x = document.getElementById("pppp").innerHTML.trim()
console.log(x)
<p id="pppp">dvshjb</p>
答案 2 :(得分:0)
P tag does not have value property. For div, p, span, etc. you can access innerHTML.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>blabla</title>
</head>
<body>
<p id="pppp">dvshjb</p>
<script>
x=document.getElementById("pppp").innerHTML
console.log(x)
</script>
</body>
</html>
答案 3 :(得分:0)
将value
替换为innerHtml
发件人:
x=document.getElementById("pppp").value;
console.log(x);
收件人:
x=document.getElementById("pppp").innerHTML;
console.log(x);
答案 4 :(得分:0)
“值”属性用于输入。您应该使用“ innerText”,如下所示:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>blabla</title>
</head>
<body>
<p id="pppp">dvshjb</p>
<script>
x=document.getElementById("pppp").innerText //assigning the value of <p> tag to x
console.log(x) //printing the value of x in console
</script>
</body>
</html>