Czy istnieje prosty sposób na zmianę tekstu elementu tylko za pomocą javascript wanilii? W poniższym kodzie pomyślałem, że użycie .textContent
, zamiast .innerHTML
spowoduje zmianę tekstu i pozostawienie obrazu.Użyj javascript do zmiany tekstu tylko w elemencie
<head>
<script>
function change_stuff() {
var div = document.getElementById('to_change');
div.textContent = "OMG...it's an image!";
}
</script>
</head>
<body>
<div id="to_change">
This is a huge block of text that I want to replace while leaving the image in place
<img src="./the_image.jpg">
</div>
<button onclick="change_stuff();">
ThE dOER!!
</button>
</body>
Mam też próbował, ale miał niewiele do sukcesu z wielu odmian to:
function change_stuff() {
var div = document.getElementById('to_change');
var text = div.textContent;
div.textContent = text.replace(text, "");
}
Każda pomoc będzie mile widziane
Nie. To jest myślenie życzeniowe. Umieść tekst w zakresie, jeśli chcesz zmienić tylko ten tekst, lub usuń tekstNode z elementu div – mplungjan