从javascript更改输入值属性

时间:2015-03-17 11:35:36

标签: javascript html

如果有人提出这个问题,我很抱歉,但经过几个小时的搜索,我找不到解决方案。

我有一段JavaScript代码,允许隐藏文本块,直到用户点击“阅读更多”按钮。

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

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;
}

<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 />
&#13;
&#13;
&#13;

正如您从上面的代码段中看到的那样,我遇到了一个问题,我不知道该怎么改变价值=&#34;&#34;属性。

2 个答案:

答案 0 :(得分:5)

只是一个错字:

document.getElementsById()

应该是:

document.getElementById()

Docs here.

当您的javascript代码有效时,最好先在控制台上查看:

enter image description here

在你的情况下,这是抛出的异常。如果您点击右侧链接js:25,则会直接指向getElementsById功能。

答案 1 :(得分:1)

function toggleMe(a){
    var e=document.getElementById(a);
    if(!e)return true;
    if(e.style.display=="none"){
        e.style.display="block"
        document.getElementById("toggle").value = "Read Less"
    } else{
        e.style.display="none"
        document.getElementById("toggle").value = "Read More"
    }
    return true;
}
<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 />