从javascript更改输入值属性

Tre*_*son 0 html javascript

如果有人问过这个问题,我很抱歉,但经过几个小时的搜索,我找不到解决办法.

我有一段JavaScript代码,允许隐藏文本块,直到用户点击read more按钮.

问题是我需要在点击后更改输入值属性,以便在崩溃后读取更多内容.

JavaScript的:

function toggleMe(a){
var e=document.getElementById(a);
if(!e)return true;
if(e.style.display=="none"){
e.style.display="block"
document.getElementsById("toggle").value = "Read Less"
}
else{
e.style.display="none"
document.getElementsById("toggle").value = "Read More"
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

<div id="Read" style="display:none">
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
 </div>
<br />
 <input id="toggle" type="button" onclick="return toggleMe('Read')" value="Read More">
 <br />
Run Code Online (Sandbox Code Playgroud)

正如你从上面的代码片段中看到的那样,我遇到了一个问题,我不知道该怎么改变value =""属性.

Don*_*own 5

只是一个错字:

document.getElementsById()
Run Code Online (Sandbox Code Playgroud)

应该:

document.getElementById()
Run Code Online (Sandbox Code Playgroud)

文档在这里.

如果你的javascript代码不起作用,那么最好先在你的控制台上查看:

在此输入图像描述

在你的情况下,这是抛出的异常.如果单击右侧链接js:25,它将直接指向该getElementsById功能.