Как изменить текст заголовка с помощью кнопки JavaScript? - PullRequest
0 голосов
/ 02 октября 2018

нуб тут.Я хочу сделать переменную из некоторого текста, чтобы при нажатии на нее старый текст становился новым текстом.

<!DOCTYPE html>
<html>
<body>

<h1 id="id01">THis is the old text</h1>

<p>Click on "My Button" to display its value attribute.</p>

<button id="myBtn" name="myname" value="myvalue" 
onclick="alert(this.value)">My Button</button>

<p>Click on "Try it" to change the value attribute of "My Button".</p>

<button onclick="myFunction()">Try it</button>

<p><b>Tip:</b> Click on "My Button" before and after you have clicked on 
"Try it".</p>

<script>
function myFunction() {
    document.getElementById("myBtn").value = "newButtonValue";
    document.getElementById("id01").value = "this is the new text";
}
</script>

</body>
</html>

Это первый пример, который я использую, но var = будет лучшей альтернативой.

1 Ответ

0 голосов
/ 02 октября 2018

Использовать innerText, а не значение.

function myFunction() {
    document.getElementById("myBtn").value = "newButtonValue";
    document.getElementById("id01").innerText = "this is the new text";
}
<h1 id="id01">THis is the old text</h1>

<p>Click on "My Button" to display its value attribute.</p>

<button id="myBtn" name="myname" value="myvalue" 
onclick="alert(this.value)">My Button</button>

<p>Click on "Try it" to change the value attribute of "My Button".</p>

<button onclick="myFunction()">Try it</button>

<p><b>Tip:</b> Click on "My Button" before and after you have clicked on 
"Try it".</p>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...