我正在使用javascript顯示按鈕單擊文本。當我點擊這個按鈕時,頁面重新加載後,文本出現然後消失。文本顯示然後在頁面重新加載時消失; javascript onclick event
這是包含點擊按鈕後的文字段落
<p class="lead" id="class"><strong style="color: red; font-size: 15px; display: none;"></strong></p>
這是我的按鈕:
<button type="submit" class="btn btn-primary" onclick="myFunction()">Search</button>
這是我的腳本
<script>
function myFunction() {
document.getElementById("class").innerHTML = "Class current balance total";
}
</script>
我怎樣才能讓文本顯示後頁面刷新後不會消失?
PS:我對JavaScript很陌生。
簡短的回答:你不能輕易做到這一點。較長的答案[這裏](https://stackoverflow.com/questions/7058634/persisting-dom-changes-after-clicking-a-link-and-then-pressing-the-back-button)。刷新的意思就是:它清潔板岩。 –
我沒有看到您的代碼有問題。我把它放在Plunker中,似乎沒有問題。 https://plnkr.co/edit/v0RKTBjk5QBN5JL5oNPl?p=preview(注意,不要使用'id ='class''。可能很難說你的'id'與'class'屬性不同)。 **編輯**如果你的意思是你的文本在你重新加載頁面後消失,那就是它應該做的。你不希望你的文字留在其他人看到。 – JustinJmnz
只是爲了澄清,你不希望刷新後文本消失? –